cs61a 2020fall lab04作业

cs61a 2020fall lab04作业

(¦3[▓▓]

LAB_SOURCE_FILE = __file__



this_file = __file__

def skip_add(n):
    """ Takes a number n and returns n + n-2 + n-4 + n-6 + ... + 0.

    >>> skip_add(5)  # 5 + 3 + 1 + 0
    9
    >>> skip_add(10) # 10 + 8 + 6 + 4 + 2 + 0
    30
    >>> # Do not use while/for loops!
    >>> from construct_check import check
    >>> # ban iteration
    >>> check(this_file, 'skip_add',
    ...       ['While', 'For'])
    True
    """
    "*** YOUR CODE HERE ***"
    if n == 1:
        return 1
    elif n <= 0:
        return 0
    else:
        return n + skip_add(n-2)


def summation(n, term):

    """Return the sum of the first n terms in the sequence defined by term.
    Implement using recursion!

    >>> summation(5, lambda x: x * x * x) # 1^3 + 2^3 + 3^3 + 4^3 + 5^3
    225
    >>> summation(9, lambda x: x + 1) # 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
    54
    >>> summation(5, lambda x: 2**x) # 2^1 + 2^2 + 2^3 + 2^4 + 2^5
    62
    >>> # Do not use while/for loops!
    >>> from construct_check import check
    >>> # ban iteration
    >>> check(this_file, 'summation',
    ...       ['While', 'For'])
    True
    """
    assert n >= 1
    "*** YOUR CODE HERE ***"
    if n == 1:
        return term(1)
    else:
        return term(n) + summation(n-1,term)


def paths(m, n):
    """Return the number of paths from one corner of an
    M by N grid to the opposite corner.

    >>> paths(2, 2)
    2
    >>> paths(5, 7)
    210
    >>> paths(117, 1)
    1
    >>> paths(1, 157)
    1
    """
    "*** YOUR CODE HERE ***"
    if m == 1 or n == 1:
        return 1
    else:
        return paths(m-1,n) + paths(m,n-1)


def max_subseq(n, t):
    """
    Return the maximum subsequence of length at most t that can be found in the given number n.
    For example, for n = 20125 and t = 3, we have that the subsequences are
        2
        0
        1
        2
        5
        20
        21
        22
        25
        01
        02
        05
        12
        15
        25
        201
        202
        205
        212
        215
        225
        012
        015
        025
        125
    and of these, the maxumum number is 225, so our answer is 225.

    >>> max_subseq(20125, 3)
    225
    >>> max_subseq(20125, 5)
    20125
    >>> max_subseq(20125, 6) # note that 20125 == 020125
    20125
    >>> max_subseq(12345, 3)
    345
    >>> max_subseq(12345, 0) # 0 is of length 0
    0
    >>> max_subseq(12345, 1)
    5
    """
    "*** YOUR CODE HERE ***"
    tmp = str(n)
    if t == 0:
        return 0
    elif t == 1:
        return int(max(tmp))
    else:
        if tmp[-1] == max(tmp) or len(tmp) <= t or (tmp[-2] < tmp[-1] and len(tmp) -1 <= t):
            return ( n % 10 + 10 * max_subseq(n//10,t-1))
        else:
            return max_subseq(n//10,t-1)
def add_chars(w1, w2):
    """
    Return a string containing the characters you need to add to w1 to get w2.

    You may assume that w1 is a subsequence of w2.

    >>> add_chars("owl", "howl")
    'h'
    >>> add_chars("want", "wanton")
    'on'
    >>> add_chars("rat", "radiate")
    'diae'
    >>> add_chars("a", "prepare")
    'prepre'
    >>> add_chars("resin", "recursion")
    'curo'
    >>> add_chars("fin", "effusion")
    'efuso'
    >>> add_chars("coy", "cacophony")
    'acphon'
    >>> from construct_check import check
    >>> # ban iteration and sets
    >>> check(LAB_SOURCE_FILE, 'add_chars',
    ...       ['For', 'While', 'Set', 'SetComp']) # Must use recursion
    True
    """
    "*** YOUR CODE HERE ***"


### 配置CS61A课程的实验环境 为了成功配置CS61A课程的实验环境,需遵循特定步骤来确保所有组件正常工作。按照官方提供的《CS61A Fall 2020 项目安装和配置指南》[^1],可以得知具体的设置流程。 #### 安装必要的软件包 首先应确认计算机上已安装Python解释器以及相关开发工具链。对于大多数操作系统而言,默认版本可能不足以支持最新特性或存在兼容性问题;因此建议下载并安装指定版本的Python发行版。 #### 获取源码仓库 通过Git或其他版本控制系统克隆官方维护的教学材料库至本地磁盘位置。这一步骤通常涉及访问GitHub页面上的公开链接地址,并执行如下命令: ```bash git clone https://github.com/Cal-CS-61A-Staff/spring21.git ``` 此操作会拉取整个学期所需的全部资源文件夹结构到当前目录下。 #### 设置虚拟环境 创建独立于全局系统的Python虚拟环境有助于隔离依赖关系冲突风险。利用`venv`模块建立新的沙盒空间,在其中激活后即可安全地管理第三方库表单而不会影响其他应用实例: ```bash python3 -m venv cs61a-env source cs61a-env/bin/activate pip install --upgrade pip setuptools wheel ``` 上述脚本片段展示了如何初始化一个新的虚拟环境,并更新内部Pip分发渠道以获取最新的打包工具集。 #### 安装额外依赖项 依据具体需求加载相应的扩展插件集合。部分练习案例或许还会调用外部API接口服务或是图形界面框架,所以务必参照文档说明补充缺失的部分。一般情况下,可以通过运行以下指令批量处理所需条目列表: ```bash pip install -r requirements.txt ``` 该命令读取同级路径下的文本清单,按顺序解析每一行记录的名字标签从而实现自动化部署过程。 #### 测试验证环节 最后但同样重要的是要检验先前所做的准备工作是否达到预期效果。打开终端窗口输入简单的交互式测试样例代码片断,观察返回结果是否符合逻辑定义范围之内。如果一切顺利,则表明已经具备开展后续实践活动的基础条件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值