python programming in context chapter 8 listings and figures
TRANSCRIPT
![Page 1: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/1.jpg)
Python Programming in Context
Chapter 8 Listings and Figures
![Page 2: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/2.jpg)
Objectives
• To understand more advanced examples of using a dictionary in Python
• To understand more advanced examples of using lists in Python
• To use pattern matching with regular expressions
• To learn how simple programs can help you solve more advanced problems
![Page 3: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/3.jpg)
Cryptanalysis
• Code Breaking• Brute force– Try all possibilities
• Frequency Analysis– Use additional information to help find the correct
decoding• Word Dictionary– Help to discover “real” words
![Page 4: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/4.jpg)
Breaking Rail Fence
• Try all possible numbers of rails• Look for the most real words
![Page 5: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/5.jpg)
Listing 8.1
def createWordDict(dname): myDict = {} myFile = open(dname, ‘r’) for line in myFile: myDict[line[:-1]] = True return myDict
![Page 6: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/6.jpg)
Listing 8.2def railBreak(cipherText): wordDict = createWordDict('wordlist.txt') cipherLen = len(cipherText) maxGoodSoFar = 0 bestGuess = "No words found in dictionary" for i in range(1,cipherLen+1): words = railDecrypt(cipherText,i) goodCount = 0 for w in words: if w in wordDict: goodCount = goodCount + 1 if goodCount > maxGoodSoFar: maxGoodSoFar = goodCount bestGuess = " ".join(words) return bestGuess
![Page 7: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/7.jpg)
Listing 8.3
def railDecrypt(cipherText,numRails): railLen = len(cipherText) // numRails solution = '' for col in range(railLen): for rail in range(numRails): nextLetter = (col + rail * railLen) solution = solution + cipherText[nextLetter] return solution.split()
![Page 8: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/8.jpg)
Letter Frequency Analysis
• How often does each letter appear in a large text?
• Representative for all texts• Use that information to help decode
![Page 9: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/9.jpg)
Figure 8.1
![Page 10: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/10.jpg)
Listing 8.4def letterFrequency(text): text = text.lower() nonletters = removeMatches(text,alphabet) nonletters = removeDupes(nonletters) text = removeMatches(text,nonletters) lcount = {} total = len(text) for ch in text: lcount[ch] = lcount.get(ch,0) + 1 for ch in lcount: lcount[ch] = lcount[ch] / total return lcount
![Page 11: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/11.jpg)
Listing 8.5
def getFreq(t):return t[1]
![Page 12: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/12.jpg)
Listing 8.6
def maybeAdd(ch,toList): if ch in 'abcdefghijklmnopqrstuvwxyz' and ch
not in toList: toList.append(ch)
![Page 13: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/13.jpg)
Neighbor Analysis
• Which letters appear most frequently next to another letter?
• Help to distinguish between common letters
![Page 14: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/14.jpg)
Listing 8.7
def neighborCount(text): nbDict = {} text = text.lower() for i in range(len(text)-1): nbList = nbDict.setdefault(text[i],[]) maybeAdd(text[i+1],nbList) nbList = nbDict.setdefault(text[i+1],[]) maybeAdd(text[i],nbList) for key in nbDict: nbDict[key] = len(nbDict[key]) return nbDict
![Page 15: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/15.jpg)
Listing 8.8
nbList = nbDict.get(text[i])if nbList == None:
nbDict[text[i]] = []nbList = nbDict[text[i]]
![Page 16: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/16.jpg)
Listing 8.9
def maybeAdd(ch,toDict): if ch in 'abcdefghijklmnopqrstuvwxyz': toDict[ch] = toDict.setdefault(ch,0) + 1
![Page 17: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/17.jpg)
Figure 8.2
![Page 18: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/18.jpg)
Listing 8.10
def sortByLen (w) return len(w)
![Page 19: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/19.jpg)
Regular Expression
• Pattern matching library• Wildcards• Replacement
![Page 20: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/20.jpg)
Listing 8.11
def checkWord(regex): resList = [] wordFile = open('wordlist.txt') for line in wordFile: if re.match(regex,line[:-1]): resList.append(line[:-1]) return resList
![Page 21: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/21.jpg)
Listing 8.12
def checkWord(unused,pattern): resList = [] wordFile = open('wordlist.txt') rePat = '['+unused+']' regex = re.sub('[a-z]',rePat,pattern) + '$' regex = regex.lower() print('matching ', regex) for line in wordFile: if re.match(regex,line[:-1]): resList.append(line[:-1]) return resList
![Page 22: Python Programming in Context Chapter 8 Listings and Figures](https://reader031.vdocument.in/reader031/viewer/2022020417/56649f305503460f94c4b718/html5/thumbnails/22.jpg)
Listing 8.13def findLetters(unused,pattern): resList = [] wordFile = open('wordlist.txt') ctLetters = re.findall('[a-z]',pattern) print(ctLetters) rePat = '(['+unused+'])' regex = re.sub('[a-z]',rePat,pattern) + '$' regex = regex.lower() for line in wordFile: myMatch = re.match(regex,line[:-1]) if myMatch: matchingLetters = myMatch.groups() matchList = [] for l in matchingLetters: matchList.append(l.upper()) resList.append(line[:-1]) resList.append(zip(ctLetters,matchList)) return resList