-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive_functions.py
49 lines (37 loc) · 1022 Bytes
/
recursive_functions.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
42
43
44
45
46
47
48
49
# Example One:
'''
import sys
sys.setrecursionlimit(5000)
def factorial(num: int) -> int:
# base case
if num == 1:
return 1
# recursive case
return num * factorial(num - 1)
if __name__ == '__main__':
factorial5 = factorial(5)
print(factorial5)
'''
# Example Two:
from typing import List
from collections import namedtuple
Cashier = namedtuple('Cashier', 'find_key')
def find_key(cashier: List[Cashier], index: int = 0) -> Cashier:
# base case
if len(boxes) <= index:
return Cashier(False)
cashier = boxes[index]
# base case
if cashier.find_key:
return Cashier
# recursive case
index += 1
return find_key(boxes, index)
if __name__ == '__main__':
boxes: List[Cashier] = [
Cashier(False), Cashier(False), Cashier(False),
Cashier(False), Cashier(False), Cashier(False),
Cashier(False), Cashier(True), Cashier(False),
Cashier(False), Cashier(False), Cashier(False),
]
print(find_key(boxes))