-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bucket.py
31 lines (25 loc) · 934 Bytes
/
Bucket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Bucket class to conduct frequency analysis on polyalphabetic cipher
with a fixed key length(vigeneres)
"""
import string
class Bucket:
#initialize the bucket with a frequency map for each letter occurrence in the bucket
def __init__(self, value):
self.value = value;
alphabet = list(string.ascii_uppercase)
initKeys = [0] * 26
self.frequencyMap = dict(zip(alphabet,initKeys))
self.letCount = 0
#return the bucket value i.e. its position in the fixed length key
def getValue(self):
return self.value
#return the bucket's frequency map
def printMap(self):
print( "Bucket ", str(self.value), " frequency analysis: ")
for key in self.frequencyMap:
print(key, self.frequencyMap[key], "\tP: ", round(self.frequencyMap[key] / self.letCount, 4))
#increment a letter occurrence in the bucket
def incMap(self, letter):
self.frequencyMap[letter] = self.frequencyMap.get(letter) + 1
self.letCount += 1