This lab deals with packages, regular expressions and file I/O.
Version 1. May 5, 2019.
Instructions:
Modify this notebook by adding the Python code described below.
Test your code using the menu item Cell ► Run All
Submit the notebook (the .ipynb file) to the appropriate dropbox on the course web site.
Import the packages json and datetime.
import json, datetime
Import the package cmath into the cm namespace (i.e. as cm). Write a function cfun(x) that returns the value of the expression $e^{jx}+1$. For example, cfun(cm.pi) should return 0.
import cmath as cm
def cfun(x):
return cm.exp(1j*x)+1
print(cfun(cm.pi))
Download the file words.txt from the web site to the same directory (folder) as this notebook. This is a text file with about 10,000 words in about 1500 lines. The words in each line are separated by spaces. Read the words in the file words.txt into a list of strings named words. The spaces should not be included in the words.
words=[w for s in open('words.txt') for w in s.split()]
Set the following string variables to the corresponding regular expressions:
re_dog to a regular expression that matches the string dog appearing at the start of a string. For example, it would match 'dogma' but not 'boondoggle'. re_at to a regular expression that matches the string 'at' appearing at the start OR end of a string. For example, it would match 'atom' and 'mat' but not 'batter're_bvc to a regular expression that matches strings that begin with the letter 'b' followed by any number of vowels (a,e,i,o or u) and ending with one letter that is not a vowel. For example, it would match 'beak' but not 'bury'.Note: the words list must be set correctly in the previous question for the answers to this question to be verified.
re_dog = r'dog'
re_at = r'^at|at$'
re_bvc = r'^b[aeiou]+[^aeiou]$'
Write a function called writecsv(fn,l) that takes two arguments. The first argument is the name of a file to be written into the current directory. The second is a list of lists. Each of the outer lists is to be written on a separate line with each item in the inner lists separated by commas. For example, writecsv('myfile.csv',[ [1,'b',3], ['a',2,'c'] ]) would create the file myfile.csv with the following lines:
1,b,3
a,2,c
You do not need to put quotes around any of the items or escape embedded commas.
def writecsv(fn,l):
f=open(fn,'w')
for s in l:
print(*s,sep=',',file=f)
f.close()
# lab validation code; do not modify
def labcheck():
def checkhash(l,n):
import hashlib
# print(hashlib.md5(''.join(l).encode('utf8')).hexdigest())
assert hashlib.md5(''.join(l).encode('utf8')).hexdigest() == n, \
'wrong values in list: {} ... {}'.format(l[0],l[-1])
def q1():
assert json.dumps and datetime.date
def q2():
import random
n=random.randint(1,10)
assert cm.pi and abs(cfun(n*cm.pi)-2*(1-n%2))<1e-6
def q3():
checkhash(words,'539b36540252cf9d09b7680643f21678')
def q4():
import re
for r,h in [(re_dog,'ef2d83005ec707a93d7fac46c54e857b'),
(re_at,'0955cfaf16f61d95ea613104b151f470'),
(re_bvc,'2adddb5156c6217e253293cc2cd1154c')]:
checkhash([s for s in words if re.search(r,s)],h)
def q5():
import random
import hashlib
l=[[l for l in random.sample('abc123',k=3)] for i in range(3)]
s=[','.join(s)+'\n' for s in l]
h=hashlib.md5(''.join(s).encode('utf8')).hexdigest()
writecsv('lab4q5.csv',l)
checkhash(open('lab4q5.csv').readlines(),h)
for i,s in ((i,'q%d'%i) for i in range(1,20)):
if s in locals():
try:
locals()[s]()
print("Question {} OK.".format(i))
except Exception as e:
print("Failed check for Question {}: {}".format(i,e))
labcheck()