[2021 Spring]CS61A 学习笔记 Homework 3: Recursion

作业说明:https://inst.eecs.berkeley.edu/~cs61a/sp21/hw/hw03/#required-questions

Q1: Num eights

Write a recursive function num_eights that takes a positive integer x and returns the number of times the digit 8 appears in x. Use recursion - the tests will fail if you use any assignment statements.
传入正整数x,返回x中数字8的个数,用递归,不能用赋值语句。
思路:要求使用递归,不能用赋值语句,那么计数只能在return语句中进行,num_eights(x // 10) + 1.
代码:

def num_eights(x):
    """Returns the number of times 8 appears as a digit of x.

    >>> num_eights(3)
    0
    >>> num_eights(8)
    1
    >>> num_eights(88888888)
    8
    >>> num_eights(2638)
    1
    >>> num_eights(86380)
    2
    >>> num_eights(12345)
    0
    >>> from construct_check import check
    >>> # ban all assignment statements
    >>> check(HW_SOURCE_FILE, 'num_eights',
    ...       ['Assign', 'AugAssign'])
    True
    """
    "*** YOUR CODE HERE ***"
    if x == 0:
        return 0
    if x % 10 == 8:
        return num_eights(x // 10) + 1
    return num_eights(x // 10)

Q2: Ping-pong


同样要求要用递归,不能用赋值语句。
代码:

def pingpong(n):
    """Return the nth element of the ping-pong sequence.

    >>> pingpong(8)
    8
    >>> pingpong(10)
    6
    >>> pingpong(15)
    1
    >>> pingpong(21)
    -1
    >>> pingpong(22)
    -2
    >>> pingpong(30)
    -2
    >>> pingpong(68)
    0
    >>> pingpong(69)
    -1
    >>> pingpong(80)
    0
    >>> pingpong(81)
    1
    >>> pingpong(82)
    0
    >>> pingpong(100)
    -6
    >>> from construct_check import check
    >>> # ban assignment statements
    >>> check(HW_SOURCE_FILE, 'pingpong', ['Assign', 'AugAssign'])
    True
    """
    "*** YOUR CODE HERE ***"

先考虑赋值语句+while循环的常规解法:

def pingpong(n):
    i = 1
    ppvalue = 0
    flag = 1
    while i <= n:
        ppvalue += flag
        if num_eights(i) or i % 8 == 0:
            flag = - flag
        i += 1
    return ppvalue

再用辅助函数替代while循环中的flag,递归调用

def pingpong(n):
    def flag(x):
        if x == 1:
            return 1
        if num_eights(x) or x % 8 == 0:
            return -flag(x-1)
        return flag(x-1)
    if n == 1:
        return 1
    return pingpong(n-1) + flag(n-1)

Q3: Missing Digits

Write the recursive function missing_digits that takes a number n that is sorted in increasing order (for example, 12289 is valid but 15362 and 98764 are not). It returns the number of missing digits in n. A missing digit is a number between the first and last digit of n of a that is not in n. Use recursion - the tests will fail if you use while or for loops.
找到逐位增长(不减)的数字中,首位和末位间缺少的数字。
先考虑最小单元 n < 10 的情况,再考虑 n < 100时(即2位数)的情况,最后递归。

def missing_digits(n):
    """Given a number a that is in sorted, increasing order,
    return the number of missing digits in n. A missing digit is
    a number between the first and last digit of a that is not in n.
    >>> missing_digits(1248) # 3, 5, 6, 7
    4
    >>> missing_digits(19) # 2, 3, 4, 5, 6, 7, 8
    7
    >>> missing_digits(1122) # No missing numbers
    0
    >>> missing_digits(123456) # No missing numbers
    0
    >>> missing_digits(3558) # 4, 6, 7
    3
    >>> missing_digits(35578) # 4, 6
    2
    >>> missing_digits(12456) # 3
    1
    >>> missing_digits(16789) # 2, 3, 4, 5
    4
    
    >>> missing_digits(4) # No missing numbers between 4 and 4
    0
    >>> from construct_check import check
    >>> # ban while or for loops
    >>> check(HW_SOURCE_FILE, 'missing_digits', ['While', 'For'])
    True
    """
    "*** YOUR CODE HERE ***"
    if n < 10:
        return 0
    if n < 100:
        return n % 10 - n // 10 - 1 if n % 10 != n // 10 else n % 10 - n // 10
    return missing_digits(n // 10) + missing_digits(n % 100)

Q4: Count coins

posted @ 2021-06-21 00:19  ikventure  阅读(3797)  评论(0编辑  收藏  举报