I'm a newbie with several questions:
I'm sure you will get several answers!
def to_lower(ciphertext): #converts ciphertext to lowercase
lower_case = ""
for character in ciphertext:
if 'A' <= character <= 'Z':
location = ord(character) - ord('A')
new_ascii = location + ord('a')
character = chr(new_ascii)
lower_case = lower_case + character
return lower_case
Hint #1: you can eliminate that entire function. ciphertext.lower()
does the same thing.
ciphertext=to_lower(ciphertext)
ciphertext = ciphertext.lower()
nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM=0,0,0,0,0,0,0,0,0,0,0,0,0
nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ=0,0,0,0,0,0,0,0,0,0,0,0,0b=1
while b<=cipherlength:
if ciphertext[b-1]=="a":
nA=nA+1
if ciphertext[b-1]=="b":
nB=nB+1
if ciphertext[b-1]=="c":
nC=nC+1
if ciphertext[b-1]=="d":
nD=nD+1
if ciphertext[b-1]=="e":
nE=nE+1...
Why do you start with b=1 if you really want to start at b=0?
Hint #2: use a dictionary. Replace the entire while loop with:
letters = ''.join( [ chr(65+i) for i in range(26) ] )
# or letters = 'abcdefghijklmnopqrstuvwxyz' if you prefer
counts = {}
for ch in letters:
counts[ch] = 0
for ch in ciphertext:
counts[ch] += 1
NumLet=0
if nA!=0:
NumLet=NumLet+1
print "there are",nA,"occurrences of the letter a"
if nB!=0:
NumLet=NumLet+1
print "there are",nB,"occurrences of the letter b"
...
Hint #3:
for ch in letters:
if counts[ch]:
NumLet += 1
print "There are",counts[ch],"occurrances of the letter",ch
L=[nA,nB,nC,nD,nE,nF,nG,nH,nI,nJ,nK,nL,nM,nN,nO,nP,nQ,nR,nS,nT,nU,nV,nW,nX,nY,nZ]
l='abcdefghijklmnopqrstuvwxyz'Max=max(L)
cipherA=''
while Max>0:
b=0
while b<26:
if L[b]==Max:
cipherA=cipherA+l[b]
b=b+1
Max=Max-1
Hint #4: Turn the dict into a list and sort the list.
L = [ (count,ch) for ch,count in counts ]
L.sort()
for count,ch in L:
print count,ch
def decrypt(cipherA,common_letters,ciphertext):
plaintext=ciphertext
b=1
while b<=NumLet:
plaintext=plaintext.replace(cipherA[b-1],common_letters[b-1])
b=b+1
return plaintext
Hint #5: consider string.translate.
def merit(plaintext,cipherlength):
merit=0
b=1
while b<=cipherlength:
if plaintext[b-1]=='E':
merit=merit+1
if plaintext[b-1]=='T':
merit=merit+1
if plaintext[b-1]=='H':
merit=merit+1
#Most Common Trigrams
if plaintext[b-1:b+2]=='THE':
merit=merit+1
if plaintext[b-1:b+2]=='ING':
merit=merit+1
if plaintext[b-1:b+2]=='AND':
merit=merit+1
if plaintext[b-1:b+2]=='HER':
merit=merit+1
Hint #6: Any time you see a construct like this in Python, you need to
think about dicts and lists. Proper programming is all about data
structures, not brute force. You can replace that 600-line function
with this:
# Each tuple is word length, merit value, list of words:
commonWords = (
( 1, 1, ('E','T','R') ),
( 2, 1, ('TH','HE','IN','ER',... ) ),
( 3, 1, ('THE','ING','AND','HER','ERE', ... ) )
( 4, 2, ('THAT','WITH','THEY','HAVE','LIST' ... ) )
....
)
def merit( plaintext, cipherlength ):
merit = 0
for b in range( cipherlength ):
for len, newmerit, wordList in commonWords:
if plaintext[b:b+len] in wordList:
merit += newmerit
return merit
···
On Mon, 24 Apr 2006 20:12:53 -0400, "Daniel Ryan" <dvryan@gmail.com> wrote:
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.