-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_recursion.py
149 lines (107 loc) · 3.64 KB
/
03_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Pseudo code is a high-level description of the problem you're trying to solve, in code.
# It's written like, but it's meant to be closer to human speech.
def look_for_key(main_box):
pile = main_box.make_a_pile_to_look_through()
while pile is not empty:
box = pile.grab_a_box()
for item in box:
if(item.is_a_box()):
pile.append(item)
elif(item.is_a_key()):
print("found the key")
# Recursion is where a function calls itself.
# def countdown(i):
# print(i)
# countdown(i-1)
# infinity loop
def countdown(i):
print(i)
if i <= 0:
return
else:
countdown(i-1)
#countdown(5)
# The Call Stack
def greet(name):
print("Hello, " + name + "!")
greet2(name)
print("greeting ready to say bye...")
bye()
def greet2(name):
print("how are you, " + name + "?")
def bye():
print("ok bye!")
# greet("andrew")
# greet2("ann")
def factorial(n):
if (n == 1):
return 1
else:
return n * factorial(n-1)
x = factorial(5)
print(x)
# Recursive function
def sum(arr):
if len(arr) == 0:
return 0
else:
return arr[0] + sum(arr[1:])
def sum(arr):
total_sum = 0 # Initialize the sum variable
for i in range(len(arr)): # Use range() to iterate through the indices of the list
total_sum += arr[i] # Add the current element to the sum
return total_sum # Return the final sum
# Test with an empty list, should return 0
# print(sum([])) # Output: 0
# # Test with a list of single element, should return the element itself
# print(sum([5])) # Output: 5
# # Test with a list of positive integers
# print(sum([1, 2, 3, 4, 5])) # Output: 15 (1 + 2 + 3 + 4 + 5 = 15)
# # Test with a list of negative integers
# print(sum([-1, -2, -3, -4, -5])) # Output: -15 (-1 + (-2) + (-3) + (-4) + (-5) = -15)
# # Test with a list containing both positive and negative integers
# print(sum([1, -2, 3, -4, 5])) # Output: 3 (1 + (-2) + 3 + (-4) + 5 = 3)
def max_index(arr):
max_val = arr[0]
max_index = 0
for i in range(len(arr)):
if arr[i] > max_val:
max_val = arr[i]
max_index = i
print(arr[max_index])
return max_index
max_index([1, 2, 3, 4, 5]) # Output: 15 (1 + 2 + 3 + 4 + 5 = 15)
def max(list):
if len(list) == 2:
return list[0] if list[0] > list[1] else list[1]
sub_max = max(list[1:])
return list[0] if list[0] > sub_max else sub_max
def max_value(lst):
if not lst: # Base case: if the list is empty, return None or raise an exception
return None # You can return None or raise an exception, depending on your preference.
elif len(lst) == 1:
return lst[0]
elif len(lst) == 2:
return lst[0] if lst[0] > lst[1] else lst[1]
mid = len(lst) // 2
left_max = max_value(lst[:mid])
right_max = max_value(lst[mid:])
return left_max if left_max > right_max else right_max
def count(list):
if list == []:
return 0
return 1 + count(list[1:])
# Test with an empty list, should return None
print(max_value([])) # Output: None
# Test with a list of a single element, should return the element itself
print(max_value([5])) # Output: 5
# Test with a list of positive integers
print(max([1, 2, 3, 4, 5])) # Output: 5 (maximum value is 5)
# Test with a list of negative integers
print(max_value([-1, -2, -3, -4, -5])) # Output: -1 (maximum value is -1)
# Test with a list containing both positive and negative integers
print(max_value([1, -2, 3, -4, 5])) # Output: 5 (maximum value is 5)
# Test with a list containing duplicate maximum values
print(max_value([5, 5, 5, 5, 5])) # Output: 5 (maximum value is 5)
# Test with a large list
print(max_value([9, 8, 7, 6, 10, 12, 15, 13, 11])) # Output: 15 (maximum value is 15)