CS61A 2022 fall HW 01: Functions, Control
创始人
2024-05-16 09:14:10
0

CS61A 2022 fall HW 01: Functions, Control

文章目录

  • CS61A 2022 fall HW 01: Functions, Control
    • Q1: A Plus Abs B
    • Q2: Two of Three
    • Q3: Largest Factor
    • Q4: Hailstone

HW01对应的是Textbook的1.1和1.2

Q1: A Plus Abs B

  • 题目:

    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.

    from operator import add, subdef 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)
    
  • solution

        if b < 0:f = subelse:f = addreturn f(a, b)
    

    在终端里面输入python ok -q a_plus_abs_b

    image-20230124194758963

    这题其实一开始我没反应过来,愣了一下,模块也能赋值给变量

    • 再验证一下这种用法

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k1wdMDJ4-1674571607986)(null)]

Q2: Two of Three

  • 题目

    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 thepositive 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 _____
    

    Hint: Consider using the max or min function:

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

    呃,写这个的时候得把copilot关掉…不然copilot秒解

    • 看到hint其实思路就比较清晰了

      把三个的平方和加起来,然后减掉最大的那个数的平方

      return i*i + j*j + k*k - max(i, j, k)**2
      

    或者还有种方法

    return min(i*i+j*j,i*i+k*k,j*j+k*k)
    
  • python ok -q two_of_three

    image-20230124195643012

Q3: Largest Factor

  • 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.

    def largest_factor(n):"""Return the largest factor of n that is smaller than n.>>> largest_factor(15) # factors are 1, 3, 55>>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 4040>>> largest_factor(13) # factor is 1 since 13 is prime1""""*** YOUR CODE HERE ***"
    

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.”

  • solution

    def largest_factor(n):"""Return the largest factor of n that is smaller than n.>>> largest_factor(15) # factors are 1, 3, 55>>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 4040>>> largest_factor(13) # factor is 1 since 13 is prime1""""*** YOUR CODE HERE ***"newlis = []for i in range(1,n):if n % i == 0:newlis.append(i)return newlis[len(newlis)-1]
    

    也可以反向做

    	factor = n - 1while factor > 0:if n % factor == 0:return factorfactor -= 1
    

image-20230124200259640

Q4: Hailstone

这个真是个经典例子, 在Linux C编程里讲到死循环的时候,还有邓俊辉《数据结构》里面讲到什么是算法的时候(考虑有穷性),都用到了这个例子

  • 题目

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

    1. Pick a positive integer n as the start.
    2. If n is even, divide it by 2.
  1. If n is odd, multiply it by 3 and add 1.
  2. Continue this process until n is 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.

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:

def hailstone(n):"""Print the hailstone sequence starting at n and return itslength.>>> a = hailstone(10)105168421>>> a7>>> b = hailstone(1)1>>> b1""""*** YOUR CODE HERE ***"

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

Note that if n == 1 initially, then the sequence is one step long.
Hint: Recall the different outputs from using regular division / and floor division //

  • solution

    一开始我补充了这个代码,好像导致死循环了…?

        length = 1while n!=1:print(n)length += 1if n % 2 == 0:n /= 2if n % 2 != 0:n = 3 * n + 1print(1)return length

    image-20230124202721068

    image-20230124202435230

    然后我改成了 n //= 2,还是死循环啊啊啊啊啊


    恍然大悟!下面这两个if 不构成选择结构,还是顺序结构!!!

    如果n执行完第一个if变成奇数的话,他还会执行第二个if…呜呜呜

            if n % 2 == 0:n /= 2if n % 2 != 0:n = 3 * n + 1
    

    另外死循环主要是看控制条件哪里出错了,所以原因就应该去n上面找,按照这个思路来

    
    def hailstone(n):"""Print the hailstone sequence starting at n and return itslength.>>> a = hailstone(10)105168421>>> a7>>> b = hailstone(1)1>>> b1""""*** YOUR CODE HERE ***"length = 1while n != 1:print(n)length += 1if n % 2 == 0:n //= 2elif n % 2 != 0:n = 3 * n + 1print(1)return length

这个ok的评测机制应该是用的python的Doctests吧🤔

orz,怎么说呢,这个作业,反正是我在大学没有的体验

image-20230124200903663

  • 最后给的这个hailstone猜想的拓展阅读材料挺有意思的
    • https://www.quantamagazine.org/mathematician-proves-huge-result-on-dangerous-problem-20191211
  • 更好玩的是它给的一个网站
    • https://www.dcode.fr/collatz-conjecture

上一篇:C++初阶--继承

下一篇:Tkinter的Listbox控件

相关内容

热门资讯

时空猎人安卓苹果系统,探索无尽... 你知道吗?最近在手机游戏圈里,有一款叫做《时空猎人》的游戏可是火得一塌糊涂呢!不管是安卓用户还是苹果...
安卓9.0系统的电视,新一代电... 亲爱的读者们,你是否也像我一样,对科技新玩意儿充满好奇?今天,我要和你聊聊一个让人眼前一亮的话题——...
小pc安装安卓系统,轻松安装安... 你有没有想过,你的小PC也能变身成为安卓系统的超级玩家呢?没错,就是那个平时默默无闻的小家伙,现在也...
高通备份安卓系统,全方位数据安... 你知道吗?在这个科技飞速发展的时代,手机备份可是个不得不提的话题。尤其是对于安卓用户来说,选择一个靠...
谷歌安卓系统有多少,从诞生到全... 你有没有想过,那个无处不在的谷歌安卓系统,究竟在全球有多少用户呢?它就像一个神秘的数字,每天都在悄悄...
fc黄金传说安卓系统,畅享复古... 你有没有听说最近安卓系统上的一款超酷的游戏——《FC黄金传说》?这款游戏可是让不少玩家都沉迷其中,今...
变小的我安卓系统,安卓系统演变... 你有没有发现,最近你的手机好像变轻了?没错,说的就是你,那个陪伴你多年的安卓系统。它悄无声息地进行了...
vivo安卓系统小彩蛋,体验科... 你知道吗?在vivo的安卓系统中,竟然隐藏着一些超有趣的小彩蛋!这些小彩蛋就像是在手机里埋下的宝藏,...
安卓系统如何强制重启,安卓系统... 手机突然卡壳了,是不是又该给它来个“大保健”了?没错,今天就来聊聊安卓系统如何强制重启。别小看这个看...
全民飞行团安卓系统,体验指尖上... 你知道吗?最近在手机游戏圈里,有个叫做“全民飞行团”的新星正在闪耀!这款游戏不仅吸引了无数玩家的目光...
安卓鸿蒙系统壁纸软件,壁纸软件... 亲爱的手机控们,你是否厌倦了单调的壁纸?想要给你的安卓手机换上充满科技感的鸿蒙系统风格壁纸?那就跟我...
安卓系统ram重新分区,提升系... 你有没有发现,你的安卓手机最近有点儿卡呢?别急,别急,今天就来给你揭秘如何给安卓系统的RAM来个重新...
迷你退出安卓系统了吗,转型新篇... 最近有没有发现你的手机上那个可爱的迷你退出图标突然不见了?别急,让我来给你揭秘迷你退出安卓系统了吗的...
华为优先使用安卓系统,打造自主... 你知道吗?最近科技圈里有个大动作,那就是华为宣布优先使用安卓系统。这可不是一个简单的决定,它背后可是...
安卓系统隐藏了设置,隐藏设置功... 你知道吗?安卓系统这个大宝藏里,竟然隐藏着一些不为人知的设置!是不是听起来就有点小激动呢?别急,今天...
反渣恋爱系统安卓,收获真爱 你有没有听说过那个神奇的“反渣恋爱系统安卓”呢?最近,这款应用在网络上可是火得一塌糊涂,不少单身狗都...
安卓出厂系统能升级,探索无限可... 你知道吗?现在这个时代,手机更新换代的速度简直就像坐上了火箭!而说到手机,安卓系统可是占据了半壁江山...
老安卓刷机系统,从入门到精通 你有没有想过,你的老安卓手机其实还有大大的潜力呢?没错,就是那个陪伴你多年的老安卓,它可不是只能用来...
安卓粉ios系统app,兼容性... 你有没有发现,身边的朋友圈里,安卓粉和iOS系统粉总是争论不休?今天,咱们就来聊聊这个话题,看看安卓...
安卓系统语言下载,探索安卓系统... 你有没有想过,你的安卓手机是不是该换换口味了?没错,就是语言!想象如果你能轻松切换到自己喜欢的语言,...