Sep-10-2021, 11:55 AM
Good day, Dear Pythonistas
I have the following problem:
Write a function sequence(n) that will print out a sequence of numbers without using a for / while loop. If the number n is given, print the next sequence of numbers without using a loop. We decrease the number n by 5 until we reach a negative number or 0.
Examples:
sequence(16)
[16, 11, 6, 1, -4]
sequence(10)
[10, 5, 0]
I tried to use an empty list and method "append" inside of a recursive function, but it doesn't work. I had a look at the following similar problem: https://www.geeksforgeeks.org/print-a-pa...-any-loop/, but it only works if we use print(). Could you help me?
My code is:
I have the following problem:
Write a function sequence(n) that will print out a sequence of numbers without using a for / while loop. If the number n is given, print the next sequence of numbers without using a loop. We decrease the number n by 5 until we reach a negative number or 0.
Examples:
sequence(16)
[16, 11, 6, 1, -4]
sequence(10)
[10, 5, 0]
I tried to use an empty list and method "append" inside of a recursive function, but it doesn't work. I had a look at the following similar problem: https://www.geeksforgeeks.org/print-a-pa...-any-loop/, but it only works if we use print(). Could you help me?
My code is:
def sequence(n): empty_list=[] if (n == 0 or n < 0): #print(n) empty_list.append(n) return else: empty_list.append(n) #print(n,end=", ") sequence(n-5) return empty_list