-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture9.py
More file actions
225 lines (162 loc) · 4.54 KB
/
lecture9.py
File metadata and controls
225 lines (162 loc) · 4.54 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
####### Lecture 9 #######
####### Recursion #######
# 1. Break a problem into smaller parts
# 2. Evaluate these sub-parts
# 3. Put everything back together
# Recursion is usually written as a function
# calling itself
# Recursion generally DOES NOT involve for
# or while loops
#########################
# String reversal
def reverseString(string):
"""returns string in reverse"""
# Base case
if string == '':
return ''
## if len(string) == 1:
## return string
# Recursive case
else:
return string[-1] + reverseString(string[:-1])
# print reverseString('hello')
#########################
# Fibonacci numbers
fibCount = 0
def fibMemo(n):
return fibonacci(n, {})
# From http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap04.html
def fibonacci(n, memoDict):
"""Calculates the nth fibonacci number"""
# Counting the number of calls
global fibCount
fibCount += 1
# Memoized solution
if n in memoDict:
return memoDict[n]
# Base cases
if n == 0 or n == 1:
return 1
# Recursive case
else:
result = fibonacci(n-1, memoDict) + fibonacci(n-2, memoDict)
# Add the result to the dictionary
memoDict[n] = result
return result
# print fibMemo(3), fibCount
# print fibMemo(36), fibCount - 5 # I know fibCount is 5 for fib(3)
###########################
# Binary search
# Given a sorted list and an element, return either an index
# where that element can be found, or None if one does not exist.
def binary_search(l, elem):
# Base case: element is not in an empty list
if len(l) == 0:
return None
# Look for element in the middle
mid = len(l)/2
if l[mid] == elem:
return mid
# If not there, look in the half that would contain elem (recurse)
if elem > l[mid]:
sub = binary_search(l[mid+1:], elem)
if sub is None:
return None
else:
return mid + 1 + sub
else: # elem < l[mid]
return binary_search(l[:mid], elem)
# print binary_search(range(10), 0)
# print binary_search(range(10), 6)
# print binary_search(range(10), 11)
# print binary_search(range(10), -2)
###########################
# Merge sort
# Sorts a list, using the merge sort algorithm.
def merge_sort(l):
# Base case: length 1 list is sorted
if len(l) == 1:
return l
# Otherwise, split into 2 subproblems, sort recursively, and merge
# split list into halves
mid = len(l)/2 # integer division on purpose!
lower = l[:mid]
upper = l[mid:]
# recursively sort halves
lower = merge_sort(lower)
upper = merge_sort(upper)
# combine the sorted results
return merge(lower, upper)
# helper method to merge already-sorted lists
def merge(l1, l2):
# if either list empty, return the other
if len(l1) == 0:
return l2;
elif len(l2) == 0:
return l1
# otherwise, pull off smaller element and keep going
else:
if l1[0] <= l2[0]:
return [l1[0]] + merge(l1[1:], l2)
return [l2[0]] + merge(l1, l2[1:])
# print merge_sort([5,4,7,2,8,4,1])
def countdown(n):
if n == 0:
print "Blastoff!"
else:
print n
countdown(n-1)
def dash(n):
print "-"*n
def dashfun(loops, size):
if loops > 0:
for a in range(1,size):
dash(a)
for a in reversed(range(1,size)):
dash(a)
dashfun(loops-1,size)
else:
return
def is_a_vowel2(char):
char = char.lower()
vowels = ['a', 'e', 'i', 'o', 'u']
if char in vowels:
return True
else:
return False
def is_a_vowel(char):
char = char.lower()
if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u':
return True
else:
return False
def count_all_vowels(sent):
count = 0
vowels = ['a', 'e', 'i', 'o', 'u']
for letter in sent:
if is_a_vowel(letter):
count += 1
return count
def sum_digits(aStr):
total = 0
strange = ['1','2','3','4','5','6','7','8','9']
for s in aStr:
if s in strange:
total += int(s)
return total
def is_palindrome(aStr):
aStr = aStr.replace(" ", "")
aStr = aStr.lower()
if aStr == aStr[::-1]:
return True
else:
return False
def most_freq_char(aStr):
max_count = 0
max_char = ""
for char in aStr:
occur = aStr.count(char)
if occur > max_count:
max_count = occur
max_char = char
return max_char