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

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

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.

img

image-20231015190938453

That’s my plan!

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

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))


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮光 掠影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值