Scheming in Python I

Consider the following class, implemented in Python:

class A(object):
    def __init__(self, a=None, b=None):
        self.a = a
        self.b = b

This class can be used to create lists, as demonstrated in this example:

 #the list 1, 2, 3
 one_two_three = A(1, A(2, A(3)))
 one_two_three.a #returns 1

Jenny wrote the below function to create a list of 1000 elements and return a sublist of the last 163 elements. To get the correct answer, she must call it with inputs n and m: get_sublist_last_163(n, m).

n and m are non-negative integers. What is the value of n + m?

def get_sublist_last_163(N, M):
    # create a list of 1000 elements
    big_list = A(1000)
    i = 999
    while (i>0):
        big_list = A(i, big_list)
        i = i - 1

    # get a sublist of the last 163 items

    sublist_last_163 = big_list
    # call "b" N times
    i = 0
    while (i < N):
        sublist_last_163 = sublist_last_163.b
        i = i + 1        

    # call "a" M times
    i = 0
    while (i < M):
        sublist_last_163 = sublist_last_163.a
        i = i + 1

    return sublist_last_163
×

Problem Loading...

Note Loading...

Set Loading...