Lab 3: Recursion, Tree Recursion(CS61A 2020)

There is no corresponding answer for lab3 on the Internet, and the author has been stuck for a long time.

The author may make some summaries of the problems he has been stuck on, and cannot cover everything. Please forgive me.

(Add the answer to this) (Full answer at the end)

Q2: WWPD: Journey to the Center of the Earth

Use Ok to test your knowledge with the following "What Would Python Display?" questions:

python3 ok -q sr-wwpd -u

For all WWPD questions, type Function if you believe the answer is <function...>Error if it errors, and Nothing if nothing is displayed.

>>> def crust():
...     print("70km")
...     def mantle():
...          print("2900km")
...          def core():
...               print("5300km")
...               return mantle()
...          return core
...     return mantle
>>> drill = crust
>>> drill = drill()

>>70km

>>> drill = drill()

>>2900km

>>> drill = drill()

>>5300km
>>2900km

>>> drill()

>>5300km
>>2900km
>>Function

1. The previous drill=drill() is just a call,

But it does not return its value or type (because it is an assignment)

2.The last drill()

It can be understood as,

Two values ​​are printed during the function call and the type of drill() is returned -->function

Q6: Ping-pong

The ping-pong sequence counts up starting from 1 and is always either counting up or counting down. At element k, the direction switches if k is a multiple of 8 or contains the digit 8. The first 30 elements of the ping-pong sequence are listed below, with direction swaps marked using brackets at the 8th, 16th, 18th, 24th, and 28th elements:

Index 1 2 3 4 5 6 7 [8] 9 10 11 12 13 14 15 [16] 17 [18] 19 20 21 22 23
PingPong Value 1 2 3 4 5 6 7 [8] 7 6 5 4 3 2 1 [0] 1 [2] 1 0 -1 -2 -3
Index (cont.) [24] 25 26 27 [28] 29 30
PingPong Value [-4] -3 -2 -1 [0] -1 -2

Implement a function pingpong that returns the nth element of the ping-pong sequence without using any assignment statements.

You may use the function num_eights, which you defined in the previous question.

Use recursion - the tests will fail if you use any assignment statements.

Hint: If you're stuck, first try implementing pingpong using assignment statements and a while statement. Then, to convert this into a recursive solution, write a helper function that has a parameter for each variable that changes values in the body of the while loop.

 Analysis: pingpong(n) Recursive return pingpong(n-1)+direction(n)

1. The function of direction() is to determine the positive or negative value of the nth value. The initial value is 1.

   When n<8, it can be determined that it must be positive, that is, 1

   When n>8, recursively count how many nodes n there are and the previous point n+1 of the node

(These two points will convert positive and negative)

(Because the positive and negative numbers between the previous point n+1 of the node and the larger node are the same

The node processing is the same as in the figure)

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 ***"
        if n <= 8:
        return n
    return direction(n) + pingpong(n-1)


def direction(n):
    if n < 8:
        return 1
    if (n-1) % 8 == 0 or num_eights(n-1):
        return -1 * direction(n-1)
    return direction(n-1)

 Complete answer code:

HW_SOURCE_FILE=__file__


def pascal(row, column):
    """Returns a number corresponding to the value at that location
    in Pascal's Triangle.
    >>> pascal(0, 0)
    1
    >>> pascal(0, 5)	# Empty entry; outside of Pascal's Triangle
    0
    >>> pascal(3, 2)	# Row 4 (1 3 3 1), 3rd entry
    3
    """
    "*** YOUR CODE HERE ***"
    if row<0 :
        return 0
    if row==0 and column==0:
        return 1
    elif column>row or column<0:
        return 0
    else:
        return pascal(row-1,column)+pascal(row-1,column-1)


def compose1(f, g):
    """"Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

def repeated(f, n):
    """Return the function that computes the nth application of func (recursively!).

    >>> add_three = repeated(lambda x: x + 1, 3)
    >>> add_three(5)
    8
    >>> square = lambda x: x ** 2
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    >>> repeated(square, 0)(5)
    5
    >>> from construct_check import check
    >>> # ban iteration
    >>> check(HW_SOURCE_FILE, 'repeated',
    ...       ['For', 'While'])
    True
    """
    "*** YOUR CODE HERE ***"
    if n==0:
        return lambda  x:x
    if n==1:
        return f
    return compose1(f,repeated(f,n-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
    else:
        return num_eights(x//10)


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 ***"
    if n <= 8:
        return n
    return direction(n) + pingpong(n-1)


def direction(n):
    if n < 8:
        return 1
    if (n-1) % 8 == 0 or num_eights(n-1):
        return -1 * direction(n-1)
    return direction(n-1)

Guess you like

Origin blog.csdn.net/2301_79140115/article/details/134789251