[Tutor] comapring lists
Jacob S.
keridee at jayco.net
Fri Dec 3 04:06:35 CET 2004
If you or anybody else is interested, I've written a script for codes like
kids in junior high use to write notes to each other with... It will cipher
and decipher mixed letters (encryption), turn words inside out (try it and
see), date code (mixed letters that changes with the date), morse code,
piglatin (primitive)... See for yourself.
import time
lowercase = 'abcdefghijklmnopqrstuvwxyz'
whitespace = '\t\n\x0b\x0c\r '
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
print """\
Types of codes are:
mixed letters
insideout
date code
morse ode
piglatin
"""
while 1:
unordo = raw_input('Are we going to decipher or cipher? ')
if unordo == 'quit':
break
type = raw_input('Which type of code would you like? ').lower()
if type == 'mixed letters':
if unordo == 'cipher':
ask = raw_input('Please type the code text using regular
language. ')
returnlist = []
copyofask = ask ## For use when checking the capitalization
later...
ask = ask.lower()
ref = lowercase
increment = raw_input('What letter would you like "a" paired
with? ')
added = increment
increment = ref.index(increment)
ask = list(ask)
for x in ask:
if x in whitespace or x in punctuation:
returnlist.append(x)
else:
ind = ref.index(x)
ind = ind+increment
while ind >= 26:
ind = ind - 26
returnlist.append(ref[ind])
returnlist.append(added)
inde = 0
for x in copyofask:
if x == x.upper():
returnlist[inde] = returnlist[inde].upper()
inde = inde+1
returnlist = "".join(returnlist)
print
print returnlist
print
if unordo == 'decipher':
ask = raw_input('Please type in the coded message. ')
returnlist = []
copyofask = ask ## For use when checking the capitalization
later...
ask = ask.lower()
ref = lowercase
ask = list(ask)
del copyofask[-1]
added = ask.pop()
increment = ref.index(added)
for x in ask:
if x in whitespace or x in punctuation:
returnlist.append(x)
else:
ind = ref.index(x)
ind = ind-increment
while ind < 0:
ind = ind+26
returnlist.append(ref[ind])
inde = 0
for x in copyofask:
if x == x.upper():
returnlist[inde] = returnlist[inde].upper()
inde = inde+1
returnlist = "".join(returnlist)
print
print returnlist
print
if type == 'insideout':
if unordo == 'cipher':
returnlist = []
ask = list(raw_input('Please type in the message. '))
while len(ask) > 0:
returnlist.append(ask.pop(0))
ask.reverse()
returnlist.reverse()
returnlist = "".join(returnlist)
print
print returnlist
print
if unordo == 'decipher':
returnlist = []
ask = list(raw_input('Please type in the message. '))
returnlist.append(ask.pop(0))
while len(ask) > 0:
returnlist.append(ask.pop(0))
returnlist.reverse()
returnlist = "".join(returnlist)
print
print returnlist
print
if type == 'date code':
if unordo == 'cipher':
ask = raw_input('Please type in the message. ')
copyofask = ask
returnlist = []
ask = list(ask.lower())
datesmall = raw_input('What is the date you want to use? ')
if datesmall == '':
datesmall = time.strftime('%m%d%y')
dateexpanded = time.strftime('%B %d, %Y')
else:
dateexpanded = datesmall
datesmall = time.strptime(datesmall,'%B %d, %Y')
datesmall = time.strftime('%m %d %y',datesmall).split(" ")
datesmall = [str(int(x)) for x in datesmall]
datesmall = list("".join(datesmall))
datesmall = [int(x) for x in datesmall]
print
print dateexpanded
t = 0
for x in ask:
if x in punctuation or x in whitespace:
returnlist.append(x)
t = t - 1
else:
m = t
while m >= len(datesmall):
m = m - len(datesmall)
start = lowercase.index(x)+datesmall[m]
if start >= 26:
start = start-26
returnlist.append(lowercase[start])
t = t+1
inde = 0
for x in copyofask:
if x in uppercase:
returnlist[inde] = returnlist[inde].upper()
inde = inde+1
returnlist = "".join(returnlist)
print returnlist
print
if unordo == 'decipher':
date = raw_input('Please type the date on the message. ')
ask = raw_input('Please type the message. ')
copyofdate = date
copyofask = ask
returnlist = []
ask = list(ask.lower())
month = time.strptime(date,'%B %d, %Y')
datesmall = time.strftime('%m %d %y',month).split(" ")
datesmall = [str(int(x)) for x in datesmall]
datesmall = list("".join(datesmall))
datesmall = [int(x) for x in datesmall]
t = 0
for x in ask:
if x in punctuation or x in whitespace:
returnlist.append(x)
t = t - 1
else:
m = t
while m >= len(datesmall):
m = m - len(datesmall)
start = lowercase.index(x)-datesmall[m]
if start >= 26:
start = start-26
returnlist.append(lowercase[start])
t = t+1
inde = 0
for x in copyofask:
if x == x.upper():
returnlist[inde] = returnlist[inde].upper()
inde = inde+1
returnlist = "".join(returnlist)
print
print copyofdate
print returnlist
print
if type == 'morse code':
morseletters =
['*-','-***','-*-*','-**','*','**-*','--*','****','**','*---','-*-','*-**','
--','-*','---','*--*','--*-','*-*','***','-','**-','***-','*--','-**-','-*--
','--**']
morsedigits =
['-----','*----','**---','***--','****-','*****','-****','--***','---**','--
--*']
if unordo == 'cipher':
ask = (raw_input('Give me text in English. ').lower())
returnlist = []
for x in ask:
if x in punctuation or x in whitespace:
returnlist.append(x)
else:
ind = lowercase.index(x)
returnlist.append(morseletters[ind])
returnlist = " ".join(returnlist).replace(" "," ]|[ ")
print
print returnlist
print
if unordo == 'decipher':
returnlist = []
words = raw_input('Please give me the morse code -- letters
seperated by one space -- words by two. ').split(' ')
for x in words:
for m in x.split(" "):
ind = morseletters.index(m)
returnlist.append(lowercase[ind])
returnlist.append(' ')
returnlist = "".join(returnlist)
print
print returnlist
print
if type == 'pig latin' or type == 'piglatin':
vowels = 'aeiouyAEIOUY'
if unordo == 'cipher':
words = raw_input('Give me text in English. ').split(' ')
for x in words:
x = list(x)
a = ''
if x[-1] in punctuation:
a = x.pop()
if x[0] in vowels:
x.append('yay')
else:
letter = x.pop(0)
x.append(letter)
x.append('ay')
x.append(a)
x = "".join(x)
ask = " ".join(words)
print
print ask
print
if unordo == 'decipher':
words = raw_input('iveGay emay hetay igPay atinLay, owNay!!!
').split(' ')
for x in words:
a = ''
x = list(x)
end = ''
if x[-1] in punctuation:
end = x.pop()
if x[-3:] == ['y','a','y']:
del x[-3:]
else:
del x[-2:]
a = x.pop()
x[0:0] = a
x.append(end)
x = "".join(x)
ask = " ".join(words)
print
print ask
print
> I am getting user input and comparing it to an iterated list. I can get
the
> input to print out verbatim. I want to then "swap" individual letters in
the
> input with letters from a second list so it is like a simple encryption.
I
> can't figure out the correct logic or syntax. Here is what I have so far:
>
> user = raw_input("Enter your selection: ")
>
>
>
> encrypt =
>
['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','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','0','1','2','3','4'
,'5','6','7','8','9','0']
>
> decrypt
>
=['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','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','A','0','1','2','3','4
','5','6','7','8','9','0']
>
>
> for i in range(len(encrypt)):
> print user[0:62]
> break
>
> for j in range(len(decrypt)):
> for j in zip(decrypt):
> print 'Encrypted-> %s' % (j,user)
> break
>
> This does not work. Any suggestions?
>
> Thanks
> Jim DeCaro
> Microsoft Certified Systems Engineer
> Windows 2000 - Windows NT 4.0 + Internet
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list