Reputation: 21
Task: Write a program that gives the user a choice to encode or decode. You will either encode letters into numbers separated by dashes, or decode a series of numbers (also separated by dashes) into letters.
I am learning python, and this Lab has proven extremely difficult because I don't yet know all the tools I need to make it function. I managed to get the encode portion to work, but my decode portion is crap. I think it is taking 2-digit numbers and treating them like individual numbers ("19" as "1" and "9", so returns "ai" instead of 's'). I have thought about switching from using indexing to trying to convert the numbers to letters using chr(), but not being familiar with that either, I keep getting type errors when trying to add 96 to get the correct number. Then encode_letters function is a bit clumsy, but it works. However, the decode_numbers function is what is giving me fits.
Can anyone tell me what I am doing wrong?
def encode_letters():
global code_out
encryption_key = (('a','1'), ('b','2'), ('c','3'), ('d','4'), ('e','5'), ('f','6'), ('g', '7'), ('h','8'), ('i','9'), ('j','10'), ('k','11'), ('l','12'),
('m','13'), ('n','14'), ('o','15'), ('p','16'), ('q','17'), ('r','18'), ('s','19'), ('t','20'), ('u','21'), ('v','22'), ('w','23'), ('x','24'),
('y','25'), ('z','26'))
msg_in = str(input("Enter the message you wish to encode:\n"))
msg_in = msg_in.lower()
from_index = 0
to_index = 1
for i in msg_in:
letter_found = False
for e in encryption_key:
if ('a' <= i and i <= 'z') and i == e[from_index]:
code_out = code_out + e[to_index] + "-"
letter_found = True
if not letter_found:
code_out = code_out + i
return code_out
def return_encoded():
global code_out
code_out = code_out.rstrip("-")
print("Your secret code is:", code_out.replace('- ', ' '))
def decode_numbers():
global string_out
encryption_key = (('a','1'), ('b','2'), ('c','3'), ('d','4'), ('e','5'), ('f','6'), ('g','7'), ('h','8'), ('i','9'), ('j','10'), ('k','11'), ('l','12'),
('m','13'), ('n','14'), ('o','15'), ('p','16'), ('q','17'), ('r','18'), ('s','19'), ('t','20'), ('u','21'), ('v','22'), ('w','23'), ('x','24'),
('y','25'), ('z','26'))
numbers_in = input("Enter the numbers separated by dashes that you wish to decode: ")
numbers_in = numbers_in.replace('-', ' ')
print(numbers_in)
from_index = 1
to_index = 0
for i in numbers_in:
number_found = False
for e in encryption_key:
if i == e[from_index]:
string_out = string_out + e[to_index]
number_found = True
if not number_found:
string_out = string_out + i
return string_out
def return_decoded():
global string_out
print("Your decoded string is: ", string_out.capitalize())
Upvotes: 2
Views: 1193
Reputation: 51643
Using a dictionary makes encryption, decription easier. Example for ceasar chiffre:
d = {}
# build the encode/decode dict
for k in range(26):
cha = chr(ord("a")+k)
d[cha] = k
d[cha.upper()] = k
d[k] = cha
print(d)
def encode(word,offset):
# dont use other things then a-zA-Z or else...
return ''.join(d[ (d[c]+offset)%26 ] for c in word)
def decode(word,offset):
return encode(word,-offset)
print(encode("abcdefg",1))
print(decode("abcdefg",-1))
print ( encode(decode("abrakadabrazzz",1),1) )
Output:
bcdefgh # encode abcdefg , +1
bcdefgh # decode abcdefg , -1
abrakadabrazzz # endoce + decode
The dictionary used looks like:
{'a': 0, 'A': 0, 0: 'a', 'b': 1, 'B': 1, 1: 'b', 'c': 2, 'C': 2, 2: 'c',
'd': 3, 'D': 3, 3: 'd', 'e': 4, 'E': 4, 4: 'e', 'f': 5, 'F': 5, 5: 'f',
'g': 6, 'G': 6, 6: 'g', 'h': 7, 'H': 7, 7: 'h', ...,
'x': 23, 'X': 23, 23: 'x', 'y': 24, 'Y': 24, 24: 'y', 'z': 25, 'Z': 25, 25: 'z'}
essentially it maps any lowercase and uppercase letter to a number and the number back to the lowercase character.
The encoding d[ (d[c]+offset)%26 ]
looks up the "number" that belongs to a character, adds the offset, uses modulo 26 to convert z+1
to a
instead of an error. Then is looks up the correct "new" character by it's number.
You can do the same for your task - you just need the mapping for character
to number
and for number
to character
.
When decoding split your string at '-'
and get the character for the numbervalue you got - when encoding go over all characters of your word and get the correct number from a dict, then '-'.join()
them.
Applied to your task:
from string import ascii_lowercase as low # "abc..xyz"
d = {}
number_for_a = ord("a")
# add the letters/numbers - upper case are 100 + lower case number
for k in low:
d[k] = str(ord(k) - number_for_a)
d[k.upper()] = str(100 + ord(k) - number_for_a)
# add the reverse mapping number to character
for k,v in list(d.items()):
d[v] = k
def encode(word):
# word for both cases - if no `-` in word its iterated character wise, else
# the word is split at '-' and any splits are put through the dictionary
if '-' in word:
return '-'.join(d[c] for c in word.split("-"))
return '-'.join(d[c] for c in word)
def decode(phrase):
return encode(phrase)
print(encode("abcdefg"))
print(decode("1-2-3-4-5-101-100-103-104-105"))
Output:
0-1-2-3-4-5-6
b-c-d-e-f-B-A-D-E-F
Upvotes: 0
Reputation: 27594
I think you can simplify this quite a bit if you keep track of the type conversions:
def convert(s, method='encode'):
if method == 'encode':
return '-'.join([str(ord(i)) for i in s])
elif method == 'decode':
return ''.join([str(chr(int(i))) for i in s.split('-')])
s = 'cats on wheels'
encoded = convert(s, method='encode')
decoded = convert(encoded, method='decode')
print(encoded) # prints 99-97-116-115-32-111-110-32-119-104-101-101-108-115
print(decoded) # prints cats on wheels
As you said, one can use ord
to convert a string to an integer, then use chr
to convert an integer back into a string. This lets us flip a string into a sequence of hyphen separated integers, then flip that sequence back into the input string
Upvotes: 1