Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC1018F: Functional Programming (Tutorial)

Similar presentations


Presentation on theme: "CSC1018F: Functional Programming (Tutorial)"— Presentation transcript:

1 CSC1018F: Functional Programming (Tutorial)
Diving into Python Ch. 16

2 Revision Exercise You are tasked with coding an “anonymization” program. This replaces names with initials in order to hide identity: To begin, read in a list of names from a file “idprotect.txt”, which contains names in the form: “Firstname” and also “Firstname Surname” on separate lines The main text should be read in from “plaintext.txt” and all occurrences of idprotect names replaced with their corresponding initial. Thus “John” becomes “J.” and “John Smith” becomes “J. S.” The final anonymous text should be output to the file “anontext.txt” Do this using regular expressions, maps and filters, where appropriate

3 Revision Solution def anonCheck(checkList, checkWord):
if checkWord in checkList: return checkWord[0] + "." else: return checkWord def anonymize(): # Read in and split up the input files idL = idf.read().split() sourceL = sourcef.read().split() dest = " ".join([anonCheck(namesL, word) for word in sourceL]) destf.write(dest) # close all files


Download ppt "CSC1018F: Functional Programming (Tutorial)"

Similar presentations


Ads by Google