0

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)
1
  • That's not recursive, that's iterative. That's a better way to solve this, but it's not recursive. Commented Dec 15, 2021 at 18:17

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

hank you ,,, one more question please is there a way to remove all zeros from each list? i tried numbers.remove(0) but it keeps showing one zero in each array
0

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]
]

Comments

0

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]

3 Comments

thank you ,,, one more question please is there a way to remove all zeros from each list?
i tried numbers.remove(0) but it keeps showing one zero in each array
@raghadss if you want to remove the zeroes, please see my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.