Skip to content

Commit

Permalink
Cleaned up code and wrote docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Færevaag committed Jun 24, 2013
1 parent ffa9835 commit 7749833
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 24 deletions.
59 changes: 35 additions & 24 deletions worksheet3/Entropy.py
Original file line number Diff line number Diff line change
@@ -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
if __name__ == '__main__':
main()
177 changes: 177 additions & 0 deletions worksheet3/README.md
Original file line number Diff line number Diff line change
@@ -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

<center>
<table>
<tr>
<td><b>Letter</b></td>
<td><b>Probability</b></td>
<td><b>Entropy</b></td>
</tr>
<tr>
<td>A</td>
<td>0.082</td>
<td>0.296</td>
</tr>
<tr>
<td>B</td>
<td>0.015</td>
<td>0.091</td>
</tr>
<tr>
<td>C</td>
<td>0.028</td>
<td>0.144</td>
</tr>
<tr>
<td>D</td>
<td>0.043</td>
<td>0.195</td>
</tr>
<tr>
<td>E</td>
<td>0.127</td>
<td>0.378</td>
</tr>
<tr>
<td>F</td>
<td>0.022</td>
<td>0.121</td>
</tr>
<tr>
<td>G</td>
<td>0.02</td>
<td>0.113</td>
</tr>
<tr>
<td>H</td>
<td>0.061</td>
<td>0.246</td>
</tr>
<tr>
<td>I</td>
<td>0.07,</td>
<td>0.269</td>
</tr>
<tr>
<td>J</td>
<td>0.002</td>
<td>0.018</td>
</tr>
<tr>
<td>K</td>
<td>0.008</td>
<td>0.056</td>
</tr>
<tr>
<td>L</td>
<td>0.04</td>
<td>0.186</td>
</tr>
<tr>
<td>M</td>
<td>0.024</td>
<td>0.129</td>
</tr>
<tr>
<td>N</td>
<td>0.067</td>
<td>0.261</td>
</tr>
<tr>
<td>O</td>
<td>0.075</td>
<td>0.280</td>
</tr>
<tr>
<td>P</td>
<td>0.019</td>
<td>0.109</td>
</tr>
<tr>
<td>Q</td>
<td>0.001</td>
<td>0.010</td>
</tr>
<tr>
<td>R</td>
<td>0.060</td>
<td>0.244</td>
</tr>
<tr>
<td>S</td>
<td>0.063</td>
<td>0.251</td>
</tr>
<tr>
<td>T</td>
<td>0.091</td>
<td>0.315</td>
</tr>
<tr>
<td>U</td>
<td>0.028</td>
<td>0.144</td>
</tr>
<tr>
<td>V</td>
<td>0.01</td>
<td>0.066</td>
</tr>
<tr>
<td>W</td>
<td>0.023</td>
<td>0.125</td>
</tr>
<tr>
<td>X</td>
<td>0.001</td>
<td>0.010</td>
</tr>
<tr>
<td>Y</td>
<td>0.02</td>
<td>0.113</td>
</tr>
<tr>
<td>Z</td>
<td>0.001</td>
<td>0.010</td>
</tr>
<tr>
<td><b>Total</b></td>
<td></td>
<td><b>4.180245</b></td>
</tr>
</table>
</center>

Results can be found in `results.csv`.


## Further Help

For further help or explanation please contact one of us by mail and
we'll be happy to help:

* Markus Faerevaag [s123692@student.dtu.dk](mailto:s123692@student.dtu.dk)
* Christian Mathias Rohde Kiaer [s123812@student.dtu.dk](mailto:s123812@student.dtu.dk)
* Jonathan Becktor [s123094@student.dtu.dk](mailto:s123094@student.dtu.dk)
26 changes: 26 additions & 0 deletions worksheet3/results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
A,0.296
B,0.091
C,0.144
D,0.195
E,0.378
F,0.121
G,0.113
H,0.246
I,0.269
J,0.018
K,0.056
L,0.186
M,0.129
N,0.261
O,0.280
P,0.109
Q,0.010
R,0.244
S,0.251
T,0.315
U,0.144
V,0.066
W,0.125
X,0.010
Y,0.113
Z,0.010

0 comments on commit 7749833

Please sign in to comment.