Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

recursive calling to print list of numbers

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading