Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions: Recursion
#3
def sequence(n: int):
    print(n)
    if n <= 0: return
    sequence(n-5)

sequence(16)
Output:
16 11 6 1 -4
OR

def sequence(n: int, arr = None):
    if not arr:
        arr = []
    arr.append(n)
    if n <= 0: return arr
    return sequence(n-5, arr)

print(*sequence(16))
Output:
16 11 6 1 -4
Yoriz write Sep-10-2021, 03:36 PM:
This looks like a homework thread, please be aware of Homework and No Effort Questions
Reply


Messages In This Thread
Functions: Recursion - by OmegaRed94 - Sep-10-2021, 11:55 AM
RE: Functions: Recursion - by deanhystad - Sep-10-2021, 12:52 PM
RE: Functions: Recursion - by OmegaRed94 - Sep-10-2021, 05:37 PM
RE: Functions: Recursion - by naughtyCat - Sep-10-2021, 03:18 PM
RE: Functions: Recursion - by OmegaRed94 - Sep-10-2021, 05:38 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020