DEV Community

Carver
Carver

Posted on

Record my CS61A2022 summer course Note homework1

Download hw01.zip. 下载 hw01.zip .

Submission: When you are done, submit with python3 ok --submit. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See Lab 0 for more instructions on submitting assignments.

提交:完成后,使用 python3 ok --submit 提交。您可以在截止日期前多次提交;只有最终提交的内容才会被评分。检查您是否已成功在 okpy.org 上提交代码。有关提交作业的更多说明,请参阅实验 0。

Using Ok: If you have any questions about using Ok, please refer to this guide.

使用 Ok:如果您对使用 Ok 有任何疑问,请参阅本指南。

Readings: You might find the following references useful:
阅读材料:您可能会发现以下参考资料很有用:

Python's operator module defines binary functions for Python's intrinsic arithmetic operators. For example, calling operator.add(2,3) is equivalent to calling the expression 2 + 3; both will return 5.

Python 的 operator 模块为 Python 的内在算术运算符定义了二进制函数。例如,调用 operator.add(2,3) 等效于调用表达式 2 + 3 ;两者都将返回 5 。

Note that when the operator module is imported into the namespace, like at the top of hw01.py, you can just call add(2,3) instead of operator.add(2,3).

请注意,当 operator 模块导入命名空间时,例如在 hw01.py 的顶部,您可以只调用 add(2,3) 而不是 operator.add(2,3) 。

Fill in the blanks in the following function for adding a to the absolute value of b, without calling abs. You may not modify any of the provided code other than the two blanks.

在以下函数中填写空白,用于将 a 添加到 b 的绝对值,而无需调用 abs 。除两个空格外,您不得修改提供的任何代码。

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 = _____
    else:
        f = _____
    return f(a, b)
Enter fullscreen mode Exit fullscreen mode

Use Ok to test your code:

使用 Ok 测试代码:

python3 ok -q a_plus_abs_b
Enter fullscreen mode Exit fullscreen mode

Q2: Two of Three Q2:

Write a function that takes three positive numbers as arguments and returns the sum of the squares of the two smallest numbers. Use only a single line for the body of the function.

编写一个函数,该函数将三个正数作为参数,并返回两个最小数字的平方和。仅对函数主体使用一行。

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 _____
Enter fullscreen mode Exit fullscreen mode

Hint: Consider using the max or min function:

提示:考虑使用 max 或 min 函数:

>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3

Use Ok to test your code:

使用 Ok 测试代码:

python3 ok -q two_of_three
Enter fullscreen mode Exit fullscreen mode

Q3: Largest Factor Q3:

Write a function that takes an integer n that is greater than 1 and returns the largest integer that is smaller than n and evenly divides n.

编写一个函数,该函数采用大于 1 的整数 n 并返回小于 n 的最大整数并平均除以 n 。

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 ***"
Enter fullscreen mode Exit fullscreen mode

Hint: To check if b evenly divides a, you can use the expression a % b == 0, which can be read as, "the remainder of dividing a by b is 0."

提示:要检查 b 是否均匀地除以 a ,您可以使用表达式 a % b == 0 ,它可以理解为“将 a 除以 b 的余数为 0”。

Use Ok to test your code:

使用 Ok 测试代码:

python3 ok -q largest_factor
Enter fullscreen mode Exit fullscreen mode

Q4: Hailstone Q4:

Douglas Hofstadter's Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle.

道格拉斯·霍夫施塔特(Douglas Hofstadter)的普利策奖获奖著作《哥德尔、埃舍尔、巴赫》(Gödel, Escher, Bach)提出了以下数学难题。

  1. Pick a positive integer n as the start. 选择一个正整数 n 作为开头。
  2. If n is even, divide it by 2. 如果 n 是偶数,则将其除以 2。
  3. If n is odd, multiply it by 3 and add 1. 如果 n 是奇数,则将其乘以 3 并加 1。
  4. Continue this process until n is 1. 继续此过程,直到 n 为 1。

The number n will travel up and down but eventually end at 1 (at least for all numbers that have ever been tried -- nobody has ever proved that the sequence will terminate). Analogously, a hailstone travels up and down in the atmosphere before eventually landing on earth.

数字 n 将上下移动,但最终以 1 结束(至少对于所有尝试过的数字 - 没有人证明序列会终止)。类似地,冰雹在最终降落在地球上之前在大气层中上下移动。

This sequence of values of n is often called a Hailstone sequence. Write a function that takes a single argument with formal parameter name n, prints out the hailstone sequence starting at n, and returns the number of steps in the sequence:

n 的值序列通常称为冰雹序列。编写一个函数,该函数采用具有形式参数名称 n 的单个参数,打印出从 n 开始的冰雹序列,并返回序列中的步数:

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 ***"
Enter fullscreen mode Exit fullscreen mode

Hailstone sequences can get quite long! Try 27. What's the longest you can find?

冰雹序列可能会很长!尝试 27。你能找到的最长的是什么?

Note that if n == 1 initially, then the sequence is one step long.

请注意,如果最初为 n == 1 ,则序列为一个步骤长。

Hint: Recall the different outputs from using regular division / and floor division //

提示:回想一下使用常规除法 / 和下限除法 // 的不同输出

Use Ok to test your code:

使用 Ok 测试代码:

python3 ok -q hailstone
Enter fullscreen mode Exit fullscreen mode

Curious about hailstones or hailstone sequences? Take a look at these articles:

对冰雹或冰雹序列感到好奇?看看这些文章:

  • Check out this article to learn more about how hailstones work! 查看这篇文章以了解有关冰雹如何工作的更多信息!
  • In 2019, there was a major development in understanding how the hailstone conjecture works for most numbers! 2019 年,在理解冰雹猜想如何适用于大多数数字方面取得了重大进展!

My Result

R1: A Plus Abs B 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 = sub  
    else:  
        f = add  
    return f(a, b)

Enter fullscreen mode Exit fullscreen mode

Q2: Two of Three 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)*max(i,j,k)

Enter fullscreen mode Exit fullscreen mode

Q3: Largest Factor 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 ***"  
    x=1  
    result=[1]  
    edge=int(n/2)  
    while(x<=edge):  
        if(n%x==0):  
            result.append(x)  
        x=x+1  
    return max(result)
Enter fullscreen mode Exit fullscreen mode

Q4: Hailstone 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 ***"  
    result = []  
    result.append(n)  
    print(n)  
    while(n!=1):  
        n=n/2 if(n%2==0)else 3*n+1  
        result.append(n)  
        print(int(n))  
    return len(result)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)