python programming in context chapter 8 listings and figures

22
Python Programming in Context Chapter 8 Listings and Figures

Upload: arthur-small

Post on 05-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python Programming in Context Chapter 8 Listings and Figures

Python Programming in Context

Chapter 8 Listings and Figures

Page 2: Python Programming in Context Chapter 8 Listings and Figures

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

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

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

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

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

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

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

Figure 8.1

Page 10: Python Programming in Context Chapter 8 Listings and Figures

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

Listing 8.5

def getFreq(t):return t[1]

Page 12: Python Programming in Context Chapter 8 Listings and Figures

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

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

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

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

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

Figure 8.2

Page 18: Python Programming in Context Chapter 8 Listings and Figures

Listing 8.10

def sortByLen (w) return len(w)

Page 19: Python Programming in Context Chapter 8 Listings and Figures

Regular Expression

• Pattern matching library• Wildcards• Replacement

Page 20: Python Programming in Context Chapter 8 Listings and Figures

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

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

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