Q1: Taxicab Distance

这个问题比较简单,street()和avenue()都已经定义好了, 直接调用计算距离即可

def taxicab(a, b):
    return abs(street(a) - street(b)) + abs(avenue(a) - avenue(b)) 

Q2: Squares only

Implement the function squares, which takes in a list of positive integers. It returns a list that contains the square roots of the elements of the original list that are perfect squares.

求列表里的完全平方数, 只需遍历一遍列表, 元素判断为完全平方数则存入list里面

def squares(s):

return [round(math.sqrt(x)) for x in s if square(round(math.sqrt(x))) == x]

Q3: G function

A mathematical function G on positive integers is defined by two cases:

G(n) = n,                                       if n <= 3
G(n) = G(n - 1) + 2 * G(n - 2) + 3 * G(n - 3),  if n > 3

Write a recursive function g that computes G(n). Then, write an iterative function g_iter that also computes G(n):

def g(n):

if n <= 3:

return n

return g(n-1) + 2*g(n-2) + 3*g(n-3)

def g_iter():

if n <= 3:
        return n
    g1, g2, g3 = 1, 2, 3
    for i in range(4, n+1):
        g4 = g3 + 2*g2 + 3*g1
        g1, g2, g3 = g2, g3, g4

return g3

Q4: Ping pong

The ping-pong sequence counts up starting from 1 and is always either counting up or counting down. At element k, the direction switches if k is a multiple of 7 or contains the digit 7. The first 30 elements of the ping-pong sequence are listed below, with direction swaps marked using brackets at the 7th, 14th, 17th, 21st, 27th, and 28th elements:

1 2 3 4 5 6 [7] 6 5 4 3 2 1 [0] 1 2 [3] 2 1 0 [-1] 0 1 2 3 4 [5] [4] 5 6

Implement a function pingpong that returns the nth element of the ping-pong sequence. Do not use any assignment statements; however, you may use def statements.

这道题只要理解了意思还是不难实现的,而且已经给出了提示,虽然不能赋值但是可以将迭代的中间变量作为函数的参数进行“伪递归"

def pingpong(n):

def iter(i, step, times):
        if times == n:
            return i
        if has_seven(times) or times % 7 == 0:
            return iter(i-step, -step, times+1)
        else:
            return iter(i+step, step, times+1)

return iter(1, 1, 1)

Q5: Count change

Once the machines take over, the denomination of every coin will be a power of two: 1-cent, 2-cent, 4-cent, 8-cent, 16-cent, etc. There will be no limit to how much a coin can be worth.

A set of coins makes change for amount if the sum of the values of the coins is amount. For example, the following sets make change for 7:

  • 7 1-cent coins
  • 5 1-cent, 1 2-cent coins
  • 3 1-cent, 2 2-cent coins
  • 3 1-cent, 1 4-cent coins
  • 1 1-cent, 3 2-cent coins
  • 1 1-cent, 1 2-cent, 1 4-cent coins

Thus, there are 6 ways to make change for 7. Write a function count_change that takes a positive integer amountand returns the number of ways to make change for amount using these coins of the future:

这道题的思路是使用dfs,将money的大小进行*2递增迭代。func(remain-money, money)表示兑换这种money, 并且剩余的钱变为remain-money; func(remain, money*2)表示不兑换这种钱, 并且选择下一种money。这两个函数加起来即为最终的兑换方式个数。

def count_change(amount):

if money > amount:
            return 0
        elif remain < 0:
            return 0
        elif remain == 0:
            return 1
        else:
            return func(remain - money, money) + func(remain, money * 2)
    return func(amount, 1)

Q6: Anonymous factorial

The recursive factorial function can be written as a single expression by using a conditional expression.

>>> fact = lambda n: 1 if n == 1 else mul(n, fact(sub(n, 1)))
>>> fact(5)
120

However, this implementation relies on the fact (no pun intended) that fact has a name, to which we refer in the body of fact. To write a recursive function, we have always given it a name using a def or assignment statement so that we can refer to the function within its own body. In this question, your job is to define fact recursively without giving it a name!

Write an expression that computes n factorial using only call expressions, conditional expressions, and lambda expressions (no assignment or def statements). Note in particular that you are not allowed to use make_anonymous_factorial in your return expression. The sub and mul functions from the operator module are the only built-in functions required to solve this problem:

这道题为extra quetion, 想了好久还是没有想出来, 在网上也没有找到答案,最后干脆放弃了。。

总结

总的来说这次hw比较简单,所以花的时间比较少

cs61A homework4相关推荐

  1. 哈夫曼信源编码matlab,HomeWork4.m

    function HomeWork4 clear ; clc; close all; %% 一.信源模块:随机生成字符串 f = fopen('十九大报告.txt','r'); f_char = fr ...

  2. CS61A 课时笔记 efficiency

    CS61A Spring 学习笔记 原文地址: http://composingprograms.com/pages/28-efficiency.html Measuring Efficiency 计 ...

  3. cs61a笔记-2020fall

    01-debug (2022.12.06) 持续更新cs61a 2020fall中的笔记:(¦3[▓▓] 部分内容使用的教材原文: Introduction Traceback (most recen ...

  4. CS61A Lab 7

    更好的阅读体验 Lab 7: Linked Lists, Trees / Tree Mutation lab07.zip What Would Python Display? Q1: WWPD: Li ...

  5. CS61A Lab 8

    更好的阅读体验 Lab 8: Midterm Review lab08.zip Due by 11:59pm on Wednesday, March 16. Starter Files Downloa ...

  6. CS61A Lab 10

    更好的阅读体验 Lab 10: Scheme lab10.zip Due by 11:59pm on Wednesday, March 30. Starter Files Download lab10 ...

  7. CS61A Lab 12

    更好的阅读体验 Lab 12: Scheme Data Abstraction lab12.zip Due by 11:59pm on Wednesday, April 13. Starter Fil ...

  8. CS61A Lab 4

    更好的阅读体验 Lab 4: Recursion, Tree Recursion lab04.zip What Would Python Do? Q1: Squared Virahanka Fibon ...

  9. CS61A Homework 7

    更好的阅读体验 Homework 7 Solutions hw07.zip Solution Files You can find the solutions in hw07.scm. Scheme ...

  10. CS61A 2022 fall lab01

    CS61A 2022 fall lab01 文章目录 CS61A 2022 fall lab01 Topics Division, Floor Div, and Modulo Functions Ca ...

最新文章

  1. AT COMMAND的命令集
  2. 近似推断包括采样和变分两种方法,前者是通过_____进行近似,后者是通过_______进行近似。
  3. 快手-中科大最新研究:利用对话式推荐解决用户冷启动问题
  4. 解决 E: Unable to correct problems, you have held broken packages. 问题
  5. 【SDOI2017】硬币游戏【KMP】【概率期望】【高斯消元】
  6. NEWS - InstallShield 2015 正式发布
  7. ntext字段的替换处理示例--全表替换(作者:邹建)
  8. 安装LabelMe出现ERROR: No matching distribution found for labelme解决方式(关闭网络代理+使用清华源)
  9. 【图像处理】基于matlab GUI自动报靶系统【含Matlab源码 814期】
  10. ThinkPad SL400 改装Win2003方法以及驱动下载列表(适用于SL500)
  11. 疟疾识别图像数据集(27000张图像,2类图像)
  12. 层次分析法(AHP)——MATLAB在数学建模中的应用(第2版)
  13. 简单聊聊01世界中编码和解码这对磨人的小妖儿
  14. win10 桌面的的文件都不见了 提示不注销保存都文件都为临时_在桌面上创建一个关机快捷方式,只需一条命令,关机速度大幅提升...
  15. Wannacry浅析
  16. Jointly Embedding Knowledge Graphs and Logical Rules
  17. dotnet 配置 Gitlab 的 Runner 做 CI 自动构建
  18. 电脑上PDF文档怎么做笔记?
  19. java按照多个分隔符分割字符串
  20. 温度场有限容积法程序入门之六:后处理.花絮.Contour Plotter and 3D Function Grapher Together - the Applet and the Souce Co

热门文章

  1. Mac怎么看剩余空间,Mac怎么看硬盘空间
  2. SECS的常用术语介绍与使用
  3. mysql原生态查询java_java使用原生MySQL实现数据的增删改查
  4. excel自定义功能区图标_自定义Excel条件格式图标
  5. docker添加加速器
  6. Xftp6-连接Linux传输文件---干货!!!(无私奉献无需积分)
  7. 数据库基础介绍(1)几种常见的数据库
  8. 【Python程序设计(七)】文件和数据格式化
  9. 业务如何驱动技术发展
  10. 上海悠悠python培训视频教程