CS 61A Fall 2023 Lecture 1 - Functions [Python] - Notes01, Lab00 and HW01

CS 61A Fall 2023 Lecture 1 - Functions [Python]

文章目录

  • CS 61A Fall 2023 Lecture 1 - Functions [Python]
    • Note 01
    • lab00
    • HW01

I am gonna make a plan for my future in advance, to consistently refine my coding skills. And this class is what I will try to finish this semester! Then I will move on to [UCB Data100: Principles and Techniques of Data Science] in my winter vacation, and take some ML and DL classes or may be some front-end courses later! Hope us a brighter future!

The notes will all be English. Welcome to learn CS 61A with me! CS 61A Fall 2023 I will demonstrate my solutions for labs and homework here for reference. There are already many solutions online for previous classes, but I’d like to try the latest one. If you find anything suspicious to you, don’t feel hesitate to contact me!

But if you are a newbie to CS, then I highly recommend this class by Mr.Peng. I believed that it can lay a solid foundation of coding. Join us through the following link, and you can contact me through qq 2965950825.

CS 61A Fall 2023 Lecture 1 - Functions [Python] - Notes01, Lab00 and HW01_第1张图片

CS 61A Fall 2023 Lecture 1 - Functions [Python] - Notes01, Lab00 and HW01_第2张图片

That’s my plan!

Week What
10.16~10.23 Lecture1: Lab0+Functions+HW1
10.24~10.30 Lecture2
10.31~11.6 Lecture3
11.7~11.13 Lecture4
11.14~11.20 Lecture5
11.21~11.28 Lecture6

Note 01

pretty easy! so I didn’t take anything.

lab00

def twenty_twenty_three():
    """Come up with the most creative expression that evaluates to 2023,
    using only numbers and the +, *, and - operators.

    >>> twenty_twenty_three()
    2023
    """
    return 1 + 2 * ((3*4*5//6)**3) + (7+8)//9 + 10 + 11

figure = twenty_twenty_three()

print(figure)

HW01

from operator import add, sub

#Q1
def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    >>> a_plus_abs_b(-1, 4)
    3
    >>> a_plus_abs_b(-1, -4)
    3
    """
    if b < 0:
        f = a-b
    else:
        f = a+b
    return f

print(a_plus_abs_b(2, 3))
print(a_plus_abs_b(2, -3))
print(a_plus_abs_b(-1, 4))
print(a_plus_abs_b(-1, -4))

def a_plus_abs_b_syntax_check():
    """Check that you didn't change the return statement of a_plus_abs_b.

    >>> # You aren't expected to understand the code of this test.
    >>> import inspect, re
    >>> re.findall(r'^\s*(return .*)', inspect.getsource(a_plus_abs_b), re.M)
    ['return f(a, b)']
    """
    # You don't need to edit this function. It's just here to check your work.

#Q2
def two_of_three(i, j, k):
    """Return m*m + n*n, where m and n are the two smallest members of the
    positive numbers i, j, and k.

    >>> two_of_three(1, 2, 3)
    5
    >>> two_of_three(5, 3, 1)
    10
    >>> two_of_three(10, 2, 8)
    68
    >>> two_of_three(5, 5, 5)
    50
    """
    return i*i + j*j + k*k - max(i,j,k)**2

print(two_of_three(10, 2, 8))
print(two_of_three(5, 5, 5))

def two_of_three_syntax_check():
    """Check that your two_of_three code consists of nothing but a return statement.

    >>> # You aren't expected to understand the code of this test.
    >>> import inspect, ast
    >>> [type(x).__name__ for x in ast.parse(inspect.getsource(two_of_three)).body[0].body]
    ['Expr', 'Return']
    """
    # You don't need to edit this function. It's just here to check your work.

#Q3
def largest_factor(n):
    """Return the largest factor of n that is smaller than n.

    >>> largest_factor(15) # factors are 1, 3, 5
    5
    >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
    40
    >>> largest_factor(13) # factor is 1 since 13 is prime
    1
    """
    "*** YOUR CODE HERE ***"
    counter = n-1
    while n%counter and counter!=1:
        counter=counter-1
    return counter

print("===Q3===")
print(largest_factor(15))
print(largest_factor(80))
print(largest_factor(13))

#Q4
def hailstone(n):
    """Print the hailstone sequence starting at n and return its
    length.

    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    >>> b = hailstone(1)
    1
    >>> b
    1
    """
    "*** YOUR CODE HERE ***"
    length = 1
    print(n)
    while n != 1:
        if n % 2 == 0:
            n = n // 2
            print(n)
        else:
            n = n*3 + 1;
            print(n)
        length = length + 1
    return length
    
print("===Q4===")
print(hailstone(10))
print("--------")
print(hailstone(1))


你可能感兴趣的:(CS,61a,2023,FALL,python,数据库)