(a) Give function to join multiple variables such as name, age, and weight, provided as strings and integers, into a single text string of the format 'NAME_AGE_WGT.txt'
(b) give code to split the above filename into variables of appropriate type (string or integer)
# (a)
name = 'Bob'
age = 30
weight = 170
fname = name+'_'+str(age)+'_'+str(weight)+'.txt'
print(fname)
Bob_30_170.txt
# (b)
name = fname.split('_')[0]
age = int(fname.split('_')[1])
weight = int(fname.split('_')[2].split('.')[0])
print(name,age,weight)
Bob 30 170
Given the following text string, demonstrate two different methods to find the word 'the'. Use one which finds the first match and uses base Python if you can, and one (from any library) which finds all matches.
text = """A green hunting cap squeezed the top of the fleshy balloon of a head. The green earflaps, full of large ears and uncut hair and the fine bristles that grew in the ears themselves, stuck out on either side like turn signals indicating two directions at once. Full, pursed lips protruded beneath the bushy black moustache and, at their corners, sank into little folds filled with disapproval and potato chip crumbs. In the shadow under the green visor of the cap Ignatius J. Reilly’s supercilious blue and yellow eyes looked down upon the other people waiting under the clock at the D.H. Holmes department store, studying the crowd of people for signs of bad taste in dress. """
text = """A green hunting cap squeezed the top of the fleshy balloon of a head. The green earflaps, full of large ears and uncut hair and the fine bristles that grew in the ears themselves, stuck out on either side like turn signals indicating two directions at once. Full, pursed lips protruded beneath the bushy black moustache and, at their corners, sank into little folds filled with disapproval and potato chip crumbs. In the shadow under the green visor of the cap Ignatius J. Reilly’s supercilious blue and yellow eyes looked down upon the other people waiting under the clock at the D.H. Holmes department store, studying the crowd of people for signs of bad taste in dress. """
# builtin methods to return location of first match
print(text.find('the'))
print(text.index('the'))
29 29
# regex method to find all matches, but doesn't tell us where
print(re.findall('the',text))
['the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the', 'the']
# this function does all, but returns an iterator ~ basically a function to find one at a time
re.finditer('the',text)
<callable_iterator at 0x2153d49cd30>
# easiest way to handle these is by converting the iterator to a list
list(re.finditer('the',text))
[<re.Match object; span=(29, 32), match='the'>, <re.Match object; span=(40, 43), match='the'>, <re.Match object; span=(128, 131), match='the'>, <re.Match object; span=(159, 162), match='the'>, <re.Match object; span=(168, 171), match='the'>, <re.Match object; span=(195, 198), match='the'>, <re.Match object; span=(294, 297), match='the'>, <re.Match object; span=(328, 331), match='the'>, <re.Match object; span=(417, 420), match='the'>, <re.Match object; span=(434, 437), match='the'>, <re.Match object; span=(453, 456), match='the'>, <re.Match object; span=(533, 536), match='the'>, <re.Match object; span=(538, 541), match='the'>, <re.Match object; span=(564, 567), match='the'>, <re.Match object; span=(577, 580), match='the'>, <re.Match object; span=(620, 623), match='the'>]
In class we used the timeit module (as a so-called "ipython magic" %timeit). Here we'd like to try using the time module, which gives more control over when to start and stop. Use the time.time() function to measure the running time of a fibanocci(n) calculation for n=30 calculation:
(a) using basic recursion implementation (b) using dynamic programming (see class notes) (c) using memoization via the @lru_cache() decorator (see class notes)
Hint: to use time.time(), save the time as a variable before and after the function calls and take the difference. Note that this time is measured in seconds so the result may be less than a second.
(d) now try increasing n. How high can you go?
def fib_recursive(n):
if n == 0: return 0
if n == 1: return 1
return fib_recursive(n-1) + fib_recursive(n-2)
def fib_dp(n):
fib_seq = [0, 1]
for i in range(2,n+1):
fib_seq.append(fib_seq[i-1] + fib_seq[i-2])
return fib_seq[n]
from functools import lru_cache
@lru_cache()
def fib_recursive_lru(n):
if n == 0: return 0
if n == 1: return 1
return fib_recursive(n-1) + fib_recursive(n-2)
import time
t0 = time.time()
fib_recursive(30)
t1 = time.time()
print(t1-t0)
t0 = time.time()
fib_dp(30)
t1 = time.time()
print(t1-t0)
t0 = time.time()
fib_recursive_lru(30)
t1 = time.time()
print(t1-t0)
0.33582568168640137 0.0 0.0
# slow way is already getting too slow at 35
t0 = time.time()
fib_recursive(35)
t1 = time.time()
print(t1-t0)
4.412997245788574
# DP approach starting to get slow around 10^6
t0 = time.time()
fib_dp(200000)
t1 = time.time()
print(t1-t0)
8.512107372283936
# LRU approach similar to DP
t0 = time.time()
fib_dp(200000)
t1 = time.time()
print(t1-t0)
8.025021076202393