Home > Software engineering >  recursive calling to print list of numbers
recursive calling to print list of numbers

Time:12-16

I'm trying to sort an array and print the output recursively until the array is = [0,0,0,0,0] but it only prints [3,2,1,0,0] ,,,,, this is what i wrote can you help to fix this isuee ,, still learning the answer should be

[4 3 2 1 0 0]
[3 2 1 0 0 0]
[2 1 0 0 0 0]
[1 0 0 0 0 0]
[0 0 0 0 0 0]





numbers=[5,4,3,2,1,1]
numbers.sort()

numbers.sort(reverse=True)

print('List sorted: ', numbers)


def list_arrays(numb):
level=numbers[0]
if len(numbers)-1 < level:
    return 0
    
else:
        numbers.pop(0);
        print(numbers)
        for i in range(len(numbers)):
            numbers[i] -= 1
        
        print(numbers)
        #list_arrays(numbers)
        
        


if __name__=='__main__':
    list_arrays(numbers)

CodePudding user response:

You have a number of problems here. First, you defined a function, but you never called the function. That's why nothing printed.

Second, you don't need "sort" immediately followed by "sort(reverse)".

Third, I don't know what you were attempting by checking the level. That doesn't seem to be related to your problem.

Fourth, you're subtracting 1 from every number, but you should only be subtracting 1 if the number is greater than 0.

Finally, you're only subtracting once. You need to repeat it until all the numbers are zero.

numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)

print('List sorted: ', numbers)

def list_arrays(numb):
    while any(numb):
        for i in range(len(numb)):
            if numb[i]:
                numb[i] -= 1     
        print(numb)

list_arrays(numbers)

In most cases, rather than modify the list in place, I would suggest creating a new list during each loop, but for this simple assignment, this will work.

To remove the zeros, you really do want to create a new list, not modify in place.

numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)

print('List sorted: ', numbers)

def list_arrays(numb):
    while numb:
        new = []
        for v in numb:
            if v > 1:
                new.append(v-1)
        numb = new
        print(numb)

list_arrays(numbers)

Or even:

numbers=[5,4,3,2,1,1]
numbers.sort(reverse=True)

print('List sorted: ', numbers)

def list_arrays(numb):
    while numb:
        numb = [v-1 for v in numb if v>1]
        print(numb)

list_arrays(numbers)

CodePudding user response:

In another comment you asked to remove all zeroes. Perhaps this is the solution you are looking for -

def run(t):
  while(t):
    yield t
    t = [x - 1 for x in t if x > 1]
mylist = [5,4,3,2,1,1]
for t in run(mylist):
  print(t)
[5, 4, 3, 2, 1, 1]
[4, 3, 2, 1]
[3, 2, 1]
[2, 1]
[1]

Or gather all lines in a single array using list -

mylist = [5,4,3,2,1,1]
print(list(run(mylist)))
[
  [5, 4, 3, 2, 1, 1],
  [4, 3, 2, 1],
  [3, 2, 1],
  [2, 1],
  [1]
]

CodePudding user response:

Maybe this is what you want?

def make_all_zero_and_print(numbers):
    while numbers[0] > 0:
        print(numbers)
        for i in range(len(numbers)):
            if numbers[i] > 0:
                numbers[i] -= 1
    print(numbers)

make_all_zero_and_print([5, 4, 3, 2, 1, 1])

Output:

[5, 4, 3, 2, 1, 1]
[4, 3, 2, 1, 0, 0]
[3, 2, 1, 0, 0, 0]
[2, 1, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
  • Related