IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> CS 61A 2020 Fall Lab 4: Recursion Tree Recursion Python Lists -> 正文阅读

[Python知识库]CS 61A 2020 Fall Lab 4: Recursion Tree Recursion Python Lists

Q1: List Indexing

For each of the following lists, what is the list indexing expression that evaluates to?7? For example, if?x = [7], then the answer would be?x[0]. You can use the interpreter or Python Tutor to experiment with your answers. If the code would cause an error, type?Error.

>>> x = [1, 3, [5, 7], 9]
x[2][1]

>>> x = [[7]]
x[0][0]

>>> x = [3, 2, 1, [9, 8, 7]]
x[3][2]

>>> x = [[3, [5, 7], 9]]
x[0][1][1]

What would Python display? If you get stuck, try it out in the Python interpreter!?

>>> lst = [3, 2, 7, [84, 83, 82]]
>>> lst[4]
Error

>>> lst[3][0]
84

Q2: Skip Add

Write a function?skip_add?that takes a single argument?n?and computes the sum of every other integer between?0?and?n. Assume?n?is non-negative.

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 <= 0:
        return 0

    return n + skip_add(n-2)

Q3: Summation

Now, write a recursive implementation of?summation, which takes a positive integer?n?and a function?term. It applies?term?to every number from?1?to?n?including?n?and returns the sum of the results.

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(n)

    return term(n) + sum(n-1, term)

?Q4: Insect Combinatorics

Consider an insect in an?M?by?N?grid. The insect starts at the bottom left corner,?(0, 0), and wants to end up at the top right corner,?(M-1, N-1). The insect is only capable of moving right or up. Write a function?paths?that takes a grid length and width and returns the number of different paths the insect can take from the start to the goal. (There is a?closed-form solution?to this problem, but try to answer it procedurally using recursion.)

?For example, the 2 by 2 grid has a total of two ways for the insect to move from the start to the goal. For the 3 by 3 grid, the insect has 6 diferent paths (only 3 are shown above).

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)   7*6*5
    210
    >>> paths(117, 1)
    1
    >>> paths(1, 157)
    1
    """
    "*** YOUR CODE HERE ***"

?待补充


Q5: Maximum Subsequence

A subsequence of a number is a series of (not necessarily contiguous) digits of the number. For example, 12345 has subsequences that include 123, 234, 124, 245, etc. Your task is to get the maximum subsequence below a certain length.

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 ***"

    if n == 0 or t == 0:
        return 0
    with_ones = max_subseq(n//10, t-1) * 10 + n%10
    not_with = max_subseq(n//10, t)
    if with_ones > not_with:
        return with_ones
    else:
        return not_with
        

Q6: Add Characters

Given two words,?w1?and?w2, we say?w1?is a subsequence of?w2?if all the letters in?w1?appear in?w2?in the same order (but not necessarily all together). That is, you can add letters to any position in?w1?to get?w2. For example, "sing" is a substring of "absorbing" and "cat" is a substring of "contrast".

Implement?add_chars, which takes in?w1?and?w2, where?w1?is a substring of?w2. This means that?w1?is shorter than?w2. It should return a string containing the characters you need to add to?w1?to get?w2.?Your solution must use recursion.

In the example above, you need to add the characters "aborb" to "sing" to get "absorbing", and you need to add "ontrs" to "cat" to get "contrast".

The letters in the string you return should be in the order you have to add them from left to right. If there are multiple characters in the?w2?that could correspond to characters in?w1, use the leftmost one. For example,?add_words("coy", "cacophony")?should return "acphon", not "caphon" because the first "c" in "coy" corresponds to the first "c" in "cacophony".

def add_chars(w1, w2):
    def helper(s,t):
        if s == t[0]:
            return len(t)
        else:
            return helper(s,t[1::])

    if not w1:
        return w2
    else:
        i = len(w2) - helper(w1[0],w2)
        return add_chars(w1[1::],w2[:i]+w2[i+1:])

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-04 12:48:17  更:2021-10-04 12:49:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 20:29:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计