首页 > 其他分享 >CS61A

CS61A

时间:2025-01-10 17:12:55浏览次数:1  
标签:return two three abs plus factor CS61A

CS61A

1.hw01

https://cs61a.org/hw/hw01/

from operator import add, sub

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 = lambda a,b : a-b
    else:
        f = lambda a,b : a+b
    return f(a, b)

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.


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 min(i, j, k)**2 + (i + j + k - max(i, j, k) - min(i, j, k))**2

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.


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 ***"
    for i in range(n-1,0,-1):
        if n % i == 0:
            return i
         


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 ***"
    len = 1
    while n != 1:
        print(n)
        if n % 2 == 0:
           n = n // 2
        else:
            n = n * 3 + 1
        len = len + 1 
    print(n)
    return len

标签:return,two,three,abs,plus,factor,CS61A
From: https://www.cnblogs.com/Alaso687/p/18664291

相关文章

  • CS61A Discussion12 Q2
    Definition: A sublist oflinkedlist s isalinkedlistofsomeoftheelementsof s inorder.Forexample, <362517> hassublists <321> and <627> butnot <567>.Definition: A linearsublist ofalinkedlistofnumbers s ......
  • [CS61A] 学习记录六 Lab2 题解思路分享
    前言观前提示,笔者写的代码答案放在GitHub仓库中,此处仅记录过程与心得。此外,请最好在尝试独立完成该任务后再看本文,否则就很可能失去了体验本项目精华的机会正文Q1:WWPD:LambdatheFree有两个问题有点意思。lambda与参数>>>b=lambdax:lambda:x#Lambdascanr......
  • CS61A
    CS61A是加州大学伯克利分校计算机科学系的一门入门课程,主要面向大一新生。该课程的核心内容是编程和计算机科学的基础知识,特别强调抽象技术作为管理程序复杂性的手段。CS61A的教学重点在于让学生掌握用程序来解决实际问题,而不关注底层的硬件细节。一、基本用法python命令py......
  • [CS61A-Fall-2020]学习记录四 Lecture4中有意思的点
    首先,本文不是总结归纳,只是记录一些有趣的知识点罢了assert课堂中在讲授函数,如frommathimportpidefarea_circle(r):returnr*r*pi但老师提出,当r为-10时,函数不会报错,于是引入assert来检测参数frommathimportpidefarea_circle(r):#参数应为正数......
  • cs61a回顾
    从1月25开始到2.20,完成第一个项目hog。总结让自己进度慢的主观因素:妄图一次阅读掌握所有知识:违反了《为什么学生不喜欢上学》中大脑不是用来思考的,它的真正作用在于使你避免思考的前提,避免让自己学习新知识感到有阻碍是最重要的。cs61a的课本难度还是有的啊!非母语阅读的困......
  • CS61A hw03 make_anoymous_factorial()
    CS61Ahw03make_anoymous_factorial()自问自答&写在前面​ 写这些是因为这道练习没写出来,刚开始看到官方的solution也没看明白,通过从答案反推之后,有了一些对lambda表达式的一些理解,在此分享,观看之前还是希望经过自己思考之后再看,毕竟聪明的你都来学cs61a了,应该已经学会独立思考......
  • CS61A: Structure and Interpretation of Computer Programs 笔记
    FunctionsEnvironmentDiagrams:左侧为Frames,右侧为Objects。Name类似变量名,它们存储在Frame中,指向各种各样的Objects,比如值或函数。一个Name同时只能指向一个Object,但可以改变自身指向,不受“类型”影响(Name根本没有固定的“类型”概念)。Assignment的过程是计算'='......
  • CS61A_Project Hog 复盘
    作为CS61A的第一个完整项目,整体难度较易。别出心裁地设计了一个又一个问答来函数编写,这些问题可以帮助你快速了解函数的功能以及输入输出。最大的困扰在于英语,翻译软件的部分失真导致有些题目一直理解不到位,解锁不了函数。题目翻译以及问题答案可以参考这位博主https://blog.c......
  • CS61A_hw09
     defmutate_reverse(link):"""MutatestheLinksothatitselementsarereversed.>>>link=Link(1)>>>mutate_reverse(link)>>>linkLink(1)>>>link=Link(1,Link(2,Link(3))......
  • CS61A_lab14_macro
     (define-macro(switchexprcases)(cons'cond(map(lambda(case)(cons(eq?(evalexpr)(carcase))(cdrcase)))cases))) 这段代码是一个用于Scheme语言的宏定义,可以将一系列的条件分支语句转化为Scheme的cond表达式。下面是具体的......