1

The output of this code is: [[2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]

def shrink(numbers, return_list=[]):
    n1 = [(x, numbers[i + 1]) for i, x in enumerate(numbers) if i < len(numbers) - 1]
    n2 = [x[1] - x[0] for x in n1]

    return_list.append(n2)

    if (len(n2) > 1):
        return shrink(n2, return_list)
    else:
        return return_list


print(shrink([2, 4, 6, 8, 10]))

Input: 2 4 6 8 10

The output of this inputted list in this code is: [[2, 2, 2, 2], [0, 0, 0], [0, 0], [0], [2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]

as you can see, the recursion repeats.

def shrink(numbers, return_list=[]):
    n1 = [(x, numbers[i + 1]) for i, x in enumerate(numbers) if i < len(numbers) - 1]
    n2 = [x[1] - x[0] for x in n1]

    return_list.append(n2)

    if (len(n2) > 1):
        return shrink(n2, return_list)
    else:
        return return_list

a = input()
b = a.split()
for i in range(len(b)):
    b[i] = int(b[i])
c = shrink(b)
print(shrink(b))

Please help me to debug/overhaul my second code in order to have an output same as the first code presented. I am only a high school student.

jps
  • 20,041
  • 15
  • 75
  • 79
R.E.F.
  • 37
  • 7

3 Answers3

1

In the last line, instead of doing print(shrink(b)), do print(c).

Since you are calling shrink(b) twice which has list in the argument,

During first call, return_list will have [[2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]

During second call, return_list will have [[2, 2, 2, 2], [0, 0, 0], [0, 0], [0],[[2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]] (i.e., return_list of first_call + return_list of second call)

Here is a link to understand why list gets keeps on adding. Why can a function modify some arguments as perceived by the caller, but not others?

Sanjana
  • 11
  • 1
0
def shrink(numbers, return_list=[]):
    n1 = [(x, numbers[i + 1]) for i, x in enumerate(numbers) if i < len(numbers) - 1]
    n2 = [x[1] - x[0] for x in n1]

    return_list.append(n2)

    if len(n2) > 1:
        return shrink(n2, return_list)
    else:
        return return_list


input_user = input("Enter data:")
separate_list = input_user.split()
for num in range(len(separate_list)):
    separate_list[num] = int(separate_list[num])
return_value = shrink(separate_list)
print(return_value)

enter image description here

jps
  • 20,041
  • 15
  • 75
  • 79
Shivam Pandey
  • 83
  • 2
  • 8
0

You are mutating the return_list argument, and calling the function twice.
(A default argument value is evaluated and bound when the function is defined, not when it is called.)
Each call appends to the same list, so your result gets doubled.

If you try this:

b = [2,4,6,8,10]
print(shrink(b))
print(shrink(b))
print(shrink(b))

you will see

[[2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]
[[2, 2, 2, 2], [0, 0, 0], [0, 0], [0], [2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]
[[2, 2, 2, 2], [0, 0, 0], [0, 0], [0], [2, 2, 2, 2], [0, 0, 0], [0, 0], [0], [2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]

A simple fix is to copy the list:

def shrink(numbers, return_list=[]):
    n1 = [(x, numbers[i + 1]) for i, x in enumerate(numbers) if i < len(numbers) - 1]
    n2 = [x[1] - x[0] for x in n1]

    result = return_list[:]
    result.append(n2)

    if (len(n2) > 1):
        return shrink(n2, result)
    else:
        return result

b = [2,4,6,8,10]
shrink(b)
print(shrink(b))

prints

[[2, 2, 2, 2], [0, 0, 0], [0, 0], [0]]


molbdnilo
  • 64,751
  • 3
  • 43
  • 82