Fall 2020 Berkeley cs61a hw04答案

def make_bank(balance):"""Returns a bank function with a starting balance. Supportswithdrawals and deposits.>>> bank = make_bank(100)>>> bank('withdraw', 40)    # 100 - 4060>>> bank('hello', 500)      # Invalid message passed in'Invalid message'>>> bank('deposit', 20)     # 60 + 2080>>> bank('withdraw', 90)    # 80 - 90; not enough money'Insufficient funds'>>> bank('deposit', 100)    # 80 + 100180>>> bank('goodbye', 0)      # Invalid message passed in'Invalid message'>>> bank('withdraw', 60)    # 180 - 60120"""def bank(message, amount):"*** YOUR CODE HERE ***"nonlocal balanceif message == 'withdraw':if amount > balance:return 'Insufficient funds'balance = balance - amountelif message == 'deposit':balance = balance + amountelse:return 'Invalid message'return balancereturn bankdef make_withdraw(balance, password):"""Return a password-protected withdraw function.>>> w = make_withdraw(100, 'hax0r')>>> w(25, 'hax0r')75>>> error = w(90, 'hax0r')>>> error'Insufficient funds'>>> error = w(25, 'hwat')>>> error'Incorrect password'>>> new_bal = w(25, 'hax0r')>>> new_bal50>>> w(75, 'a')'Incorrect password'>>> w(10, 'hax0r')40>>> w(20, 'n00b')'Incorrect password'>>> w(10, 'hax0r')"Frozen account. Attempts: ['hwat', 'a', 'n00b']">>> w(10, 'l33t')"Frozen account. Attempts: ['hwat', 'a', 'n00b']">>> type(w(10, 'l33t')) == strTrue""""*** YOUR CODE HERE ***"wrong_trail_password = []wrong_trail_times = 0def withdraw(amount, trail_password):nonlocal balance, wrong_trail_timesif wrong_trail_times == 3:return "Frozen account. Attempts: " + str(wrong_trail_password)if password == trail_password:if amount > balance:return 'Insufficient funds'balance = balance - amountreturn balanceelse:wrong_trail_times += 1if wrong_trail_times <= 3:wrong_trail_password.append(trail_password)return 'Incorrect password'return withdrawdef repeated(t, k):"""Return the first value in iterator T that appears K times in a row. Iterate through the items such thatif the same iterator is passed into repeated twice, it continues in the second call at the point it left offin the first.>>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])>>> repeated(s, 2)9>>> s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])>>> repeated(s2, 3)8>>> s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])>>> repeated(s, 3)2>>> repeated(s, 3)5>>> s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])>>> repeated(s2, 3)2"""assert k > 1"*** YOUR CODE HERE ***"count = 1current_number = next(t)next_number = next(t)while True:if current_number == next_number:count += 1if count == k:return next_numbercurrent_number, next_number = next_number, next(t)else:count = 1current_number, next_number = next_number, next(t)
def permutations(seq):"""Generates all permutations of the given sequence. Each permutation is alist of the elements in SEQ in a different order. The permutations may beyielded in any order.>>> perms = permutations([100])>>> type(perms)<class 'generator'>>>> next(perms)[100]>>> try: #this piece of code prints "No more permutations!" if calling next would cause an error...     next(perms)... except StopIteration:...     print('No more permutations!')No more permutations!>>> sorted(permutations([1, 2, 3])) # Returns a sorted list containing elements of the generator[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]>>> sorted(permutations((10, 20, 30)))[[10, 20, 30], [10, 30, 20], [20, 10, 30], [20, 30, 10], [30, 10, 20], [30, 20, 10]]>>> sorted(permutations("ab"))[['a', 'b'], ['b', 'a']]""""*** YOUR CODE HERE ***"if len(seq) == 1:yield seqelse:for element in permutations([x for x in seq if x != seq[0]]):for k in range(len(element) + 1):yield element[:k] + [seq[0]] + element[k:]def make_joint(withdraw, old_pass, new_pass):"""Return a password-protected withdraw function that has joint access tothe balance of withdraw.>>> w = make_withdraw(100, 'hax0r')>>> w(25, 'hax0r')75>>> make_joint(w, 'my', 'secret')'Incorrect password'>>> j = make_joint(w, 'hax0r', 'secret')>>> w(25, 'secret')'Incorrect password'>>> j(25, 'secret')50>>> j(25, 'hax0r')25>>> j(100, 'secret')'Insufficient funds'>>> j2 = make_joint(j, 'secret', 'code')>>> j2(5, 'code')20>>> j2(5, 'secret')15>>> j2(5, 'hax0r')10>>> j2(25, 'password')'Incorrect password'>>> j2(5, 'secret')"Frozen account. Attempts: ['my', 'secret', 'password']">>> j(5, 'secret')"Frozen account. Attempts: ['my', 'secret', 'password']">>> w(5, 'hax0r')"Frozen account. Attempts: ['my', 'secret', 'password']">>> make_joint(w, 'hax0r', 'hello')"Frozen account. Attempts: ['my', 'secret', 'password']"""""*** YOUR CODE HERE ***"def remainders_generator(m):"""Yields m generators. The ith yielded generator yields natural numbers whoseremainder is i when divided by m.>>> import types>>> [isinstance(gen, types.GeneratorType) for gen in remainders_generator(5)][True, True, True, True, True]>>> remainders_four = remainders_generator(4)>>> for i in range(4):...     print("First 3 natural numbers with remainder {0} when divided by 4:".format(i))...     gen = next(remainders_four)...     for _ in range(3):...         print(next(gen))First 3 natural numbers with remainder 0 when divided by 4:4812First 3 natural numbers with remainder 1 when divided by 4:159First 3 natural numbers with remainder 2 when divided by 4:2610First 3 natural numbers with remainder 3 when divided by 4:3711""""*** YOUR CODE HERE ***"def naturals():"""A generator function that yields the infinite sequence of naturalnumbers, starting at 1.>>> m = naturals()>>> type(m)<class 'generator'>>>> [next(m) for _ in range(10)][1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"""i = 1while True:yield ii += 1

Fall 2020 Berkeley cs61a hw04答案相关推荐

  1. Fall 2020 Berkeley cs61a hw01答案

    Fall 2020 Berkeley cs61a hw01答案 from operator import add, subdef a_plus_abs_b(a, b):""&quo ...

  2. Fall 2020 Berkeley cs61a Hog Project

    ** Fall 2020 Berkeley cs61a Hog Project ** Fall 2020 的Hog和之前project有些变化,Github找不到,所以分享一下- "&quo ...

  3. 2020年高压电工答案解析及高压电工考试平台

    题库来源:安全生产模拟考试一点通公众号小程序 2020年高压电工答案解析及高压电工考试平台,包含高压电工答案解析答案和解析及高压电工考试平台练习.由安全生产模拟考试一点通公众号结合国家高压电工考试最新 ...

  4. 2020年高压电工答案解析及高压电工新版试题

    题库来源:安全生产模拟考试一点通公众号小程序 2020年高压电工答案解析及高压电工新版试题,包含高压电工答案解析答案和解析及高压电工新版试题练习.由安全生产模拟考试一点通公众号结合国家高压电工考试最新 ...

  5. 2020 UCB CS61A FALL -- project1 hog

    UCB2020的秋季课,老师是Hany Farid和John DeNero hog是第一个项目,做的时候感觉细节挺多的,主要是熟悉下python的一些语法和高阶函数的使用 代码 "" ...

  6. 2020 android面试题答案

    (1)java面试题(基础+进阶)(必须) java中==和equals和hashCode的区别 ==是运算符,用来比较两个值.两个对象的内存地址是否相等: equals是Object类的方法,默认情 ...

  7. 2020年计算机考试题答案,2020年《计算机绘图》考试试题附全答案【完整版】

    <2020年<计算机绘图>考试试题附全答案[完整版]>由会员分享,可在线阅读,更多相关<2020年<计算机绘图>考试试题附全答案[完整版](6页珍藏版)> ...

  8. 2020中考计算机统考答案,2020中考必读 | 中考电脑阅卷流程“潜规则”!读完孩子少丢20分!(转给中考生)...

    今天和大家来聊一聊考试阅卷的那些事情... 现在的考试都趋于正规.公平化,特别是中考和高考这两场重要的升学考试,全国统一采取电子阅卷的方式来给学生阅卷. 而大部分平时的模拟考试都不是电脑阅卷,又加上很 ...

  9. 组合求解器 + 深度学习 =?这篇ICLR 2020论文告诉你答案

    2020-01-26 20:17:46 选自TowadsDataScience 作者:Marin Vlastelica Pogančić 机器之心编译 参与:郭元晨.魔王 如何将组合求解器无缝融入深度 ...

  10. 2020大学计算机知到答案,2020年_知到_大学计算机(济南大学)_网课答案

    摘要: ... 确定设备更新的依据是(  ). A:设备的自然寿命 B:设备的技术寿命 C:设备的经济寿命 D:设备的有效寿命 正确答案:C 答案解析:在设备更新分析中,经济寿命是确定设备最优更新期的 ...

最新文章

  1. Python 第三方库自动安装脚本
  2. kaggle训练模型
  3. Python学习笔记(二)
  4. Spring整合Hibernate中自动建表
  5. Asp.net就业课之Ado.net第一次课
  6. 如何在Chrome工具栏中固定和取消固定扩展程序
  7. EXT.NET复杂布局(二)——报表
  8. arcgis的python接口_arcgis-Python的ArcGIS API-Esri Screenshots
  9. iphone录屏没内部声音_安卓手机如何录屏?手机高清录屏指南
  10. Java生成指定范围随机数的方法
  11. linux nand 坏块_韦东山-NAND 上面都是坏块怎么办啊? - 百问网嵌入式问答社区
  12. QT窗口与Windows系统窗口之间关系和转换
  13. 万字长文带你快速了解并上手Testcontainers
  14. mysql lookup3,引用函数(三):lookup
  15. 高等数学(第七版)同济大学 总习题七 (前4题)个人解答
  16. 51系列单片机IO模试设置
  17. 小学计算机小组兴趣活动计划,计算机兴趣小组活动计划
  18. moocpython程序设计答案_中国大学MOOC免费答案_Python程序设计答案第七章节答案
  19. 魔兽插件是用php吗,【图片】手把手教你制作自己的界面【魔兽插件吧】_百度贴吧...
  20. Java解压文件Zip,War,Tar,TarGz格式

热门文章

  1. tbSchedule 使用
  2. 淘宝定时任务 tbschedule实战
  3. 直升机的扭力与反扭力
  4. SSM项目转Springboot项目
  5. spring学习-01编译spring5.0源码(亲测可用)
  6. 【uniapp小程序】—— APP项目云打包(安卓)
  7. 华为太极magisk安装教程_华为(HUAWEI)ROM安装包合集
  8. 直播app源代码 直播软件开发的iOS直播推流之h264/aac 硬编码
  9. win10专业版占多少空间_win10正常占用磁盘多大空间?
  10. 常用DateUtil