diff --git a/worksheet3/Entropy.py b/worksheet3/Entropy.py index ff9a3b4..b129728 100644 --- a/worksheet3/Entropy.py +++ b/worksheet3/Entropy.py @@ -1,33 +1,44 @@ +# +# Project 2 +# +# 01435 Practiacal Cryptanalysis +# Technical University of Denmark + +# Markus Faerevaag (s123692@student.dtu.dk) +# Christian Mathias Rohde Kiaer (s123812@student.dtu.dk) +# Jonathan Becktor (s123094@student.dtu.dk) +# + import math -letters=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] -sum =0 -def find_perc(s): +LETTERS = ['A','B','C','D','E', + 'F','G','H','I','J', + 'K','L','M','N','O', + 'P','Q','R','S','T', + 'U','V','W','X','Y','Z'] +PERC = [0.082, 0.015, 0.028, 0.043, 0.127, + 0.022, 0.02, 0.061, 0.07, 0.002, + 0.008, 0.04, 0.024, 0.067, 0.075, + 0.019, 0.001, 0.060, 0.063, 0.091, + 0.028, 0.01, 0.023, 0.001, 0.02, 0.001] - perc=[0.082,0.015,0.028,0.043,0.127,0.022,0.02,0.061,0.07,0.002,0.008,0.04,0.024,0.067,0.075,0.019,0.001,0.060,0.063,0.091,0.028,0.01,0.023,0.001,0.02,0.001] - letter=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] - pos = 0 - temp = s.upper() - if temp in letter: - for x in xrange(1,len(letter)): - if temp==letter[x]: - pos = x - return perc[pos] +def calc_entropy(letter): + """Calculates entropy for given letter""" + P = PERC[LETTERS.index(letter.upper())] + return P * math.log(1/P)/math.log(2) -def calc_ent(s): - P=find_perc(s) - sum=0 - temp = math.log(1/P)/math.log(2) - #temp = (-P*(math.log(P)/math.log(2)))-((1-P)*(math.log(1-P)/math.log(2))) - sum=P*temp - print sum - return sum +def main(): + # Sum entropy for each letter + sum = 0 + for letter in LETTERS: + entropy = calc_entropy(letter) + sum += entropy + print "%s: %.3f" % (letter, entropy) + print "The average number of bits: %f" % sum -for x in xrange(0,25): - sum=sum+calc_ent(letters[x]) - print x -print "The min bit is : %f"%sum \ No newline at end of file +if __name__ == '__main__': + main() diff --git a/worksheet3/README.md b/worksheet3/README.md new file mode 100644 index 0000000..8345027 --- /dev/null +++ b/worksheet3/README.md @@ -0,0 +1,177 @@ +Worksheet 3 +=========== + +Script for calculating the average number of bits required to store one letter of British English if perfect compression. + + +## Usage + +Simply: + + $ python Entropy.py + + +## Logic + +Sum the entropy of each character in the alphabet using the given +probability in the slides. + + +## Results + +
Letter | +Probability | +Entropy | +
A | +0.082 | +0.296 | +
B | +0.015 | +0.091 | +
C | +0.028 | +0.144 | +
D | +0.043 | +0.195 | +
E | +0.127 | +0.378 | +
F | +0.022 | +0.121 | +
G | +0.02 | +0.113 | +
H | +0.061 | +0.246 | +
I | +0.07, | +0.269 | +
J | +0.002 | +0.018 | +
K | +0.008 | +0.056 | +
L | +0.04 | +0.186 | +
M | +0.024 | +0.129 | +
N | +0.067 | +0.261 | +
O | +0.075 | +0.280 | +
P | +0.019 | +0.109 | +
Q | +0.001 | +0.010 | +
R | +0.060 | +0.244 | +
S | +0.063 | +0.251 | +
T | +0.091 | +0.315 | +
U | +0.028 | +0.144 | +
V | +0.01 | +0.066 | +
W | +0.023 | +0.125 | +
X | +0.001 | +0.010 | +
Y | +0.02 | +0.113 | +
Z | +0.001 | +0.010 | +
Total | ++ | 4.180245 | +