Python Programming in Context Chapter 8 Listings and

  • Slides: 22
Download presentation
Python Programming in Context Chapter 8 Listings and Figures

Python Programming in Context Chapter 8 Listings and Figures

Objectives • To understand more advanced examples of using a dictionary in Python •

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

Cryptanalysis • Code Breaking • Brute force – Try all possibilities • Frequency Analysis

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

Breaking Rail Fence • Try all possible numbers of rails • Look for the

Breaking Rail Fence • Try all possible numbers of rails • Look for the most real words

Listing 8. 1 def create. Word. Dict(dname): my. Dict = {} my. File =

Listing 8. 1 def create. Word. Dict(dname): my. Dict = {} my. File = open(dname, ‘r’) for line in my. File: my. Dict[line[: -1]] = True return my. Dict

Listing 8. 2 def rail. Break(cipher. Text): word. Dict = create. Word. Dict('wordlist. txt')

Listing 8. 2 def rail. Break(cipher. Text): word. Dict = create. Word. Dict('wordlist. txt') cipher. Len = len(cipher. Text) max. Good. So. Far = 0 best. Guess = "No words found in dictionary" for i in range(1, cipher. Len+1): words = rail. Decrypt(cipher. Text, i) good. Count = 0 for w in words: if w in word. Dict: good. Count = good. Count + 1 if good. Count > max. Good. So. Far: max. Good. So. Far = good. Count best. Guess = " ". join(words) return best. Guess

Listing 8. 3 def rail. Decrypt(cipher. Text, num. Rails): rail. Len = len(cipher. Text)

Listing 8. 3 def rail. Decrypt(cipher. Text, num. Rails): rail. Len = len(cipher. Text) // num. Rails solution = '' for col in range(rail. Len): for rail in range(num. Rails): next. Letter = (col + rail * rail. Len) solution = solution + cipher. Text[next. Letter] return solution. split()

Letter Frequency Analysis • How often does each letter appear in a large text?

Letter Frequency Analysis • How often does each letter appear in a large text? • Representative for all texts • Use that information to help decode

Figure 8. 1

Figure 8. 1

Listing 8. 4 def letter. Frequency(text): text = text. lower() nonletters = remove. Matches(text,

Listing 8. 4 def letter. Frequency(text): text = text. lower() nonletters = remove. Matches(text, alphabet) nonletters = remove. Dupes(nonletters) text = remove. Matches(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

Listing 8. 5 def get. Freq(t): return t[1]

Listing 8. 5 def get. Freq(t): return t[1]

Listing 8. 6 def maybe. Add(ch, to. List): if ch in 'abcdefghijklmnopqrstuvwxyz' and ch

Listing 8. 6 def maybe. Add(ch, to. List): if ch in 'abcdefghijklmnopqrstuvwxyz' and ch not in to. List: to. List. append(ch)

Neighbor Analysis • Which letters appear most frequently next to another letter? • Help

Neighbor Analysis • Which letters appear most frequently next to another letter? • Help to distinguish between common letters

Listing 8. 7 def neighbor. Count(text): nb. Dict = {} text = text. lower()

Listing 8. 7 def neighbor. Count(text): nb. Dict = {} text = text. lower() for i in range(len(text)-1): nb. List = nb. Dict. setdefault(text[i], []) maybe. Add(text[i+1], nb. List) nb. List = nb. Dict. setdefault(text[i+1], []) maybe. Add(text[i], nb. List) for key in nb. Dict: nb. Dict[key] = len(nb. Dict[key]) return nb. Dict

Listing 8. 8 nb. List = nb. Dict. get(text[i]) if nb. List == None:

Listing 8. 8 nb. List = nb. Dict. get(text[i]) if nb. List == None: nb. Dict[text[i]] = [] nb. List = nb. Dict[text[i]]

Listing 8. 9 def maybe. Add(ch, to. Dict): if ch in 'abcdefghijklmnopqrstuvwxyz': to. Dict[ch]

Listing 8. 9 def maybe. Add(ch, to. Dict): if ch in 'abcdefghijklmnopqrstuvwxyz': to. Dict[ch] = to. Dict. setdefault(ch, 0) + 1

Figure 8. 2

Figure 8. 2

Listing 8. 10 def sort. By. Len (w) return len(w)

Listing 8. 10 def sort. By. Len (w) return len(w)

Regular Expression • Pattern matching library • Wildcards • Replacement

Regular Expression • Pattern matching library • Wildcards • Replacement

Listing 8. 11 def check. Word(regex): res. List = [] word. File = open('wordlist.

Listing 8. 11 def check. Word(regex): res. List = [] word. File = open('wordlist. txt') for line in word. File: if re. match(regex, line[: -1]): res. List. append(line[: -1]) return res. List

Listing 8. 12 def check. Word(unused, pattern): res. List = [] word. File =

Listing 8. 12 def check. Word(unused, pattern): res. List = [] word. File = open('wordlist. txt') re. Pat = '['+unused+']' regex = re. sub('[a-z]', re. Pat, pattern) + '$' regex = regex. lower() print('matching ', regex) for line in word. File: if re. match(regex, line[: -1]): res. List. append(line[: -1]) return res. List

Listing 8. 13 def find. Letters(unused, pattern): res. List = [] word. File =

Listing 8. 13 def find. Letters(unused, pattern): res. List = [] word. File = open('wordlist. txt') ct. Letters = re. findall('[a-z]', pattern) print(ct. Letters) re. Pat = '(['+unused+'])' regex = re. sub('[a-z]', re. Pat, pattern) + '$' regex = regex. lower() for line in word. File: my. Match = re. match(regex, line[: -1]) if my. Match: matching. Letters = my. Match. groups() match. List = [] for l in matching. Letters: match. List. append(l. upper()) res. List. append(line[: -1]) res. List. append(zip(ct. Letters, match. List)) return res. List