-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL5_problems.py
318 lines (254 loc) · 6.62 KB
/
L5_problems.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import string
__author__ = 'Sam Broderick'
def iterPower(base, exp):
"""
base: int or float.
exp: int >= 0
returns: int or float, base^exp
:param base:
:param exp:
"""
# Your code here
if exp == 0:
return 1
elif exp > 0:
answer = base
for n in range(1, exp):
answer *= base
return answer
else:
print('Error, exponent is not 0 or positive integer.')
return
print('iterPower')
print(iterPower(2, 10))
print(iterPower(3, 3))
print(iterPower(4, 4))
def recurPower(base, exp):
"""
base: int or float.
exp: int >= 0
returns: int or float, base^exp
:param exp:
:param base:
"""
# Your code here
if exp == 0:
return 1
elif exp > 0:
return base * recurPower(base, exp - 1)
else:
print('error')
return
print('recurPower')
print(recurPower(2, 10))
print(recurPower(3, 3))
print(recurPower(4, 4))
def recurPowerNew(base, exp):
"""
base: int or float.
exp: int >= 0
returns: int or float; base^exp
:param exp:
:param base:
"""
# Your code here
if exp == 0:
return 1
elif exp > 1 and exp % 2 == 0:
return recurPowerNew(base * base, exp / 2)
elif exp > 0 and exp % 2 == 1:
return base * recurPowerNew(base, exp - 1)
else:
print('error')
return
print('recurPowerNew')
print(recurPowerNew(2, 10))
print(recurPowerNew(3, 3))
print(recurPowerNew(4, 4))
def gcdIter(a, b):
"""
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
:param b:
:param a:
"""
# Your code here
if a > b:
gcd = b
elif b >= a:
gcd = a
else:
print('Error')
while (a % gcd != 0 or b % gcd != 0) and gcd > 1:
gcd -= 1
return gcd
print('gcdIter')
print(gcdIter(2, 3))
print(gcdIter(2, 12))
print(gcdIter(6, 12))
print(gcdIter(9, 12))
print(gcdIter(17, 12))
def gcdRecur(a, b):
"""
a, b: positive integers
returns: a positive integer, the greatest common divisor of a & b.
:param b:
:param a:
"""
# Your code here
if b == 0:
return a
elif b != 0:
return gcdRecur(b, a % b)
else:
print('Error')
return
print('gcdRecur')
print(gcdRecur(2, 3))
print(gcdRecur(2, 12))
print(gcdRecur(6, 12))
print(gcdRecur(9, 12))
print(gcdRecur(17, 12))
def lenIter(aStr):
"""
aStr: a string
returns: int, the length of aStr
:param aStr:
"""
# Your code here
n = 0
for char in aStr:
n += 1
return n
print('lenIter')
print(lenIter('Hello, world!'))
def lenRecur(aStr):
"""
aStr: a string
returns: int, the length of aStr
:param aStr:
"""
# Your code here
if aStr == '':
return 0
else:
return 1 + lenRecur(aStr[1:])
print('lenRecur')
print(lenRecur('Hello, world!'))
'''My submission'''
# def isIn(char, aStr):
# '''
# char: a single character
# aStr: an alphabetized string
#
# returns: True if char is in aStr; False otherwise
# '''
# # Your code here
#
# if char == '' or aStr == '':
# return False
# else:
# bisection = int(len(aStr)/2)
# char_bisection = aStr[bisection]
# if len(aStr) == 1 and char != char_bisection:
# return False
# elif char == char_bisection:
# return True
# elif char > char_bisection:
# return isIn(char, aStr[bisection:len(aStr)])
# elif char < char_bisection:
# return isIn(char, aStr[0:bisection])
# else:
# return None
''' Instructor Solution - more elegant'''
def isIn(char, aStr):
"""
char: a single character
aStr: an alphabetized string
returns: True if char is in aStr; False otherwise
:param aStr:
:param char:
"""
# Base case: If aStr is empty, we did not find the char.
if aStr == '':
return False
# Base case: if aStr is of length 1, just see if the chars are equal
if len(aStr) == 1:
return aStr == char
# Base case: See if the character in the middle of aStr equals the
# test character
midIndex = len(aStr) / 2
midChar = aStr[midIndex]
if char == midChar:
# We found the character!
return True
# Recursive case: If the test character is smaller than the middle
# character, recursively search on the first half of aStr
elif char < midChar:
return isIn(char, aStr[:midIndex])
# Otherwise the test character is larger than the middle character,
# so recursively search on the last half of aStr
else:
return isIn(char, aStr[midIndex + 1:])
my_ascii = string.ascii_letters[::-1]
print('isIn')
print(isIn('H', my_ascii))
print(isIn('Z', my_ascii[:51]))
print(isIn('a', ''))
def semordnilapWrapper(str1, str2):
# A single-length string cannot be semordnilap
"""
Compares 2 strings if they are palindromes
:type str1: string - 1st input string
:type str2: string - 2nd input string
"""
if len(str1) == 1 or len(str2) == 1:
return False
# Equal strings cannot be semordnilap
if str1 == str2:
return False
return semordnilap(str1, str2)
def semordnilap(str1, str2):
"""
str1: a string
str2: a string
returns: True if str1 and str2 are semordnilap;
False otherwise.
:rtype: Boolean - is palindrome?
:param str1:
:param str2:
"""
# Your code here
# Why isn't this in the wrapper function?
# Maybe because it doesn't interfere, but I imagine that it
# costs cycles to open another function, so why not wrap it?
# Reject strings of unequal length
if len(str1) != len(str2):
return False
# One character left means str1 and str2 are palindromes since they the
# original single-character case was excluded.
if len(str1) == 1 and len(str2) == 1:
return True
Test = (str1[0] == str2[len(str2)-1])
if Test and semordnilap(str1[1:], str2[:-1]):
return True
else:
return False
print('semordnilap')
print(semordnilapWrapper('nametag', 'gateman'))
print(semordnilapWrapper('blues', 'hockey'))
print(semordnilapWrapper('desserts', 'stressed'))
def fibMetered(x):
global numCalls
numCalls += 1
if x == 0 or x == 1:
return 1
else:
return fibMetered(x-1) + fibMetered(x-2)
def testFib(n):
global numCalls
numCalls = 0
for i in range(n+1):
print('fib of ' + str(i) + ' = ' + str(fibMetered(i)))
print ('fib called ' + str(numCalls) + ' times')
testFib(10)