cs61A homework4
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 amount
and 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相关推荐
- 哈夫曼信源编码matlab,HomeWork4.m
function HomeWork4 clear ; clc; close all; %% 一.信源模块:随机生成字符串 f = fopen('十九大报告.txt','r'); f_char = fr ...
- CS61A 课时笔记 efficiency
CS61A Spring 学习笔记 原文地址: http://composingprograms.com/pages/28-efficiency.html Measuring Efficiency 计 ...
- cs61a笔记-2020fall
01-debug (2022.12.06) 持续更新cs61a 2020fall中的笔记:(¦3[▓▓] 部分内容使用的教材原文: Introduction Traceback (most recen ...
- CS61A Lab 7
更好的阅读体验 Lab 7: Linked Lists, Trees / Tree Mutation lab07.zip What Would Python Display? Q1: WWPD: Li ...
- CS61A Lab 8
更好的阅读体验 Lab 8: Midterm Review lab08.zip Due by 11:59pm on Wednesday, March 16. Starter Files Downloa ...
- CS61A Lab 10
更好的阅读体验 Lab 10: Scheme lab10.zip Due by 11:59pm on Wednesday, March 30. Starter Files Download lab10 ...
- CS61A Lab 12
更好的阅读体验 Lab 12: Scheme Data Abstraction lab12.zip Due by 11:59pm on Wednesday, April 13. Starter Fil ...
- CS61A Lab 4
更好的阅读体验 Lab 4: Recursion, Tree Recursion lab04.zip What Would Python Do? Q1: Squared Virahanka Fibon ...
- CS61A Homework 7
更好的阅读体验 Homework 7 Solutions hw07.zip Solution Files You can find the solutions in hw07.scm. Scheme ...
- CS61A 2022 fall lab01
CS61A 2022 fall lab01 文章目录 CS61A 2022 fall lab01 Topics Division, Floor Div, and Modulo Functions Ca ...
最新文章
- AT COMMAND的命令集
- 近似推断包括采样和变分两种方法,前者是通过_____进行近似,后者是通过_______进行近似。
- 快手-中科大最新研究:利用对话式推荐解决用户冷启动问题
- 解决 E: Unable to correct problems, you have held broken packages. 问题
- 【SDOI2017】硬币游戏【KMP】【概率期望】【高斯消元】
- NEWS - InstallShield 2015 正式发布
- ntext字段的替换处理示例--全表替换(作者:邹建)
- 安装LabelMe出现ERROR: No matching distribution found for labelme解决方式(关闭网络代理+使用清华源)
- 【图像处理】基于matlab GUI自动报靶系统【含Matlab源码 814期】
- ThinkPad SL400 改装Win2003方法以及驱动下载列表(适用于SL500)
- 疟疾识别图像数据集(27000张图像,2类图像)
- 层次分析法(AHP)——MATLAB在数学建模中的应用(第2版)
- 简单聊聊01世界中编码和解码这对磨人的小妖儿
- win10 桌面的的文件都不见了 提示不注销保存都文件都为临时_在桌面上创建一个关机快捷方式,只需一条命令,关机速度大幅提升...
- Wannacry浅析
- Jointly Embedding Knowledge Graphs and Logical Rules
- dotnet 配置 Gitlab 的 Runner 做 CI 自动构建
- 电脑上PDF文档怎么做笔记?
- java按照多个分隔符分割字符串
- 温度场有限容积法程序入门之六:后处理.花絮.Contour Plotter and 3D Function Grapher Together - the Applet and the Souce Co