-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.py
41 lines (30 loc) · 993 Bytes
/
recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# • Recursion is when a function calls itself.
# • Every recursive function has two cases: the base case and the recursive case.
# • A stack has two operations: push and pop.
# • All function calls go onto the call stack.
# • The call stack can get very large, which takes up a lot of memory.
def find_key(box):
"""
Finds the key within a nested box structure.
Args:
box (list): The nested list of boxes to search through.
Returns:
str: A message indicating whether the key was found or not.
"""
for item in box:
if item.is_a_box():
find_key(item)
elif item.is_a_key():
return "found the key"
def factorial(x : int ) -> int:
"""
Calculates the factorial of a given integer using recursion.
Args:
x (int): The integer for which to calculate the factorial.
Returns:
int: The factorial of the given integer.
Examples:
>>> factorial(5)
120
"""
return 1 if x == 1 else x * factorial(x - 1)