[2021 Spring] CS61A Discussion 3: Recursion, Tree Recursion

discussion 3: https://inst.eecs.berkeley.edu/~cs61a/sp21/disc/disc03/

Recursion

Q2: Merge Numbers

def merge(n1, n2):
    """ Merges two numbers by digit in decreasing order
    >>> merge(31, 42)
    4321
    >>> merge(21, 0)
    21
    >>> merge (21, 31) 
    3211
    """
    "*** YOUR CODE HERE ***"
    # 最小数字一定是两个数字个位中的一个
    if n1 == 0 and n2:
        return n2
    elif n2 == 0 and n1:
        return n1
    elif n1 % 10 <= n2 % 10:
        return merge(n1 // 10, n2) * 10 + n1 % 10
    else:
        return merge(n1, n2 // 10) * 10 + n2 % 10

Q3: Is Prime

def is_prime(n):
    """Returns True if n is a prime number and False otherwise.
 
    >>> is_prime(2)
    True
    >>> is_prime(16)
    False
    >>> is_prime(521)
    True
    """
    "*** YOUR CODE HERE ***"
    def is_factor(x):
        if x >= n:
            return True
        if x < n and n % x == 0:
            return False
        return is_factor(x + 1)
    return is_factor(2)

Q4: (Tutorial) Warm Up: Recursive Multiplication

def multiply(m, n):
    """ Takes two positive integers and returns their product using recursion.
    >>> multiply(5, 3)
    15
    """
    "*** YOUR CODE HERE ***"
    if m == 0 or n == 0:
        return 0
    elif m == 1:
        return n
    elif n == 1:
        return m
    elif m >= n:
        return multiply(m, n - 1) + m
    else:
        return multiply(m - 1, n) + n

Q5: (Tutorial) Recursive Hailstone

def hailstone(n):
    """Print out the hailstone sequence starting at n, and return the number of elements in the sequence
    >>> a = hailstone(10)
    10
    5
    16
    8
    4
    2
    1
    >>> a
    7
    """
    "*** YOUR CODE HERE ***"
    m = 1
    def count(n, res):
        print(n)
        if n == 1:
            return res 
        elif n % 2 == 0:
            return count(n // 2, res + 1)
        else:
            return count(n * 3 + 1, res + 1)
    return count(n, m)

Tree Recursion

Q6: Count Stair Ways

def count_stair_ways(n):
    """Returns the number of ways to climb up a flight of
    n stairs, moving either 1 step or 2 steps at a time.
    >>> count_stair_ways(4)
    5
    """
    "*** YOUR CODE HERE ***"
    if n == 0:
        return 1
    elif n == 1:
        return 1 
    else:
        return count_stair_ways(n - 1) + count_stair_ways(n - 2)

Q7: (Tutorial) Count K

参考青蛙跳台阶问题。

def count_k(n, k):
    """ Counts the number of paths up a flight of n stairs 
    when taking up to and including k steps at a time. 
    >>> count_k(3, 3) # 3, 2 + 1, 1 + 2, 1 + 1 + 1
    4
    >>> count_k(4, 4)
    8
    >>> count_k(10, 3)
    274
    >>> count_k(300, 1) # Only one step at a time
    1
    """
    "*** YOUR CODE HERE ***"
    if n == 0 or n == 1:
        return 1
    elif k == 1:
        return 1
    elif k >= n:
        return 2 * count_k(n - 1, n)
    else:
        return 2 * count_k(n - 1, k) - count_k(n - 1 - k, k)
posted @ 2021-06-23 22:01  ikventure  阅读(970)  评论(0编辑  收藏  举报