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控件

相关内容

热门资讯

安卓子系统windows11,... 你知道吗?最近科技圈可是炸开了锅,因为安卓子系统在Windows 11上的兼容性成了大家热议的话题。...
电脑里怎么下载安卓系统,电脑端... 你有没有想过,你的电脑里也能装上安卓系统呢?没错,就是那个让你手机不离手的安卓!今天,就让我来带你一...
索尼相机魔改安卓系统,魔改系统... 你知道吗?最近在摄影圈里掀起了一股热潮,那就是索尼相机魔改安卓系统。这可不是一般的改装,而是让这些专...
安卓系统哪家的最流畅,安卓系统... 你有没有想过,为什么你的手机有时候像蜗牛一样慢吞吞的,而别人的手机却能像风一样快?这背后,其实就是安...
安卓最新系统4.42,深度解析... 你有没有发现,你的安卓手机最近是不是有点儿不一样了?没错,就是那个一直在默默更新的安卓最新系统4.4...
android和安卓什么系统最... 你有没有想过,你的安卓手机到底是用的是什么系统呢?是不是有时候觉得手机卡顿,运行缓慢,其实跟这个系统...
平板装安卓xp系统好,探索复古... 你有没有想过,把安卓系统装到平板上,再配上XP系统,这会是怎样一番景象呢?想象一边享受着安卓的便捷,...
投影仪装安卓系统,开启智能投影... 你有没有想过,家里的老式投影仪也能焕发第二春呢?没错,就是那个曾经陪你熬夜看电影的“老伙计”,现在它...
安卓系统无线车载carplay... 你有没有想过,开车的时候也能享受到苹果设备的便利呢?没错,就是那个让你在日常生活中离不开的iOS系统...
谷歌安卓8系统包,系统包解析与... 你有没有发现,手机更新换代的速度简直就像坐上了火箭呢?这不,最近谷歌又发布了安卓8系统包,听说这个新...
微软平板下软件安卓系统,开启全... 你有没有想过,在微软平板上也能畅享安卓系统的乐趣呢?没错,这就是今天我要跟你分享的神奇故事。想象你手...
coloros是基于安卓系统吗... 你有没有想过,手机里的那个色彩斑斓的界面,背后其实有着一个有趣的故事呢?没错,我要说的就是Color...
安卓神盾系统应用市场,一站式智... 你有没有发现,手机里的安卓神盾系统应用市场最近可是火得一塌糊涂啊!这不,我就来给你好好扒一扒,看看这...
黑莓平板安卓系统升级,解锁无限... 亲爱的读者们,你是否还记得那个曾经风靡一时的黑莓手机?那个标志性的全键盘,那个独特的黑莓体验,如今它...
安卓文件系统采用华为,探索高效... 你知道吗?最近安卓系统在文件管理上可是有了大动作呢!华为这个科技巨头,竟然悄悄地给安卓文件系统来了个...
深度系统能用安卓app,探索智... 你知道吗?现在科技的发展真是让人惊叹不已!今天,我要给你揭秘一个超级酷炫的话题——深度系统能用安卓a...
安卓系统的分区类型,深度解析存... 你有没有发现,你的安卓手机里藏着不少秘密?没错,就是那些神秘的分区类型。今天,就让我带你一探究竟,揭...
安卓系统铠无法兑换,揭秘无法兑... 最近是不是有很多小伙伴在玩安卓系统的游戏,突然发现了一个让人头疼的问题——铠无法兑换!别急,今天就来...
汽车安卓系统崩溃怎么刷,一键刷... 亲爱的车主朋友们,你是否曾遇到过汽车安卓系统崩溃的尴尬时刻?手机系统崩溃还能重启,但汽车系统崩溃了,...
miui系统可以刷安卓p系统吗... 亲爱的手机控们,你是否对MIUI系统情有独钟,同时又对安卓P系统的新鲜功能垂涎欲滴?今天,就让我带你...