CS61A 20fall hw01

这篇博客总结了CS61A课程20fall学期的第一份作业,涉及Python编程的基础知识点。内容包括理解非纯函数副作用,比较with_if_statement()和with_if_function()的不同,使用//去除小数,以及a_plus_abs_b、two_of_three、largest_factor等函数的实现。此外,还探讨了if_function和with_if_statement的用法,并介绍了hailstone问题。
摘要由CSDN通过智能技术生成

总结:

  • Q5:非纯函数副作用;with_if_statement()和with_if_function()区别。
  • Q6:用 // 去掉小数,print(n)和count+=1与if平级。

 

Q2: a_plus_abs_b

from operator import add, mul
def a_plus_abs_b(a, b):
    """Retrun a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, -3)
    5
    >>> a_plus_abs_b(2, 3)
    5
    """
    if b < 0:
        return add(a, -b)
    else:
        return add(a, b)

 

Q3: two_of_three

def two_of_three(x,y,z):
    """Return a*a + b*b, when a and b are the two smallest members of the positive numbers x, y, and z.

    >>> two_of_three(1, 2, 3)
    5
    >>> two_of_three(3, 4, 5)
    25
    >>> two_of_three(5, 5, 5)
    50
    """
    return x**2 + y**2 +z**2 - max(x,y,z)**2
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值