[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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM