Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
414 changes: 414 additions & 0 deletions my-code/Draft of Encoding.ipynb

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions my-code/Encoding_Decoding_Functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 6 15:18:52 2020

@author: Alt-E-K
"""
#we import the ditionaries.
from Refactoring_Dictionaries import encode_phase3 as encoding_dictionary
from Refactoring_Dictionaries import decoded_dict as decoding_dictionary

#we define the encoder function. since it can encode virtually any sign there is no need
#to control for the user input. It simply goes charachter by charachter translating with the
#dictionary, puts it in a list and returns the joined strings.
def ultimate_encoder():
new_list=[]
message=input('Please introduce the message for encoding: ')
for i in message:
#here we can change the encryption level by changing the dictionary from wich we encode, so the code is flexible
new_char=encoding_dictionary.get(i)
new_s=str(new_char)
new_list.append(new_s)
joined_str = ("".join(new_list))
print(joined_str)

#before the decoding function we need to define an
#we create the 2 empty lists and the one with all the dictionary keys for checking if
#translation is possible.

def improved_decoder():
broke_down=[]
translated = []
our_dict_list= list(decoding_dictionary.keys())
#making sure that all the inputs are strings
message=str(input('Please introduce the message for decoding:'))


#breaking the string down into parts to check if we can translate it, then translate it
for i in range(0, len(message), 3):
char=message[i:i+3]
broke_down.append(char)

#making sure that the input is in OUR encoding method. the wile loop is made to ensure infinite oportunities for inputting
#the correct code are available
while message != None:
if set(broke_down).issubset(set(our_dict_list)):
#if it is it will proceed to translate, print and break the loop
for i in broke_down:
tr_char=decoding_dictionary.get(i)
translated.append(tr_char)
joined_str = ("".join(translated))
print(joined_str)
break
else:
message = str(input('We are not able to decode a different encoding algorithm, please introduce a valid code:'))
broke_down = []
for i in range(0, len(message), 3):
char=message[i:i+3]
broke_down.append(char)


continue
#this else made sure that when the code wasnt ours you could re input a message to decode and restarted the process
#of breaking it into pieces to check if it was ours once it has made that whole process the continue remits us back
#to the begining of the while loop and the translation process.
#However in the final function we had to change the order of the conditions, because the break from the enclosing
#while loop of the other function needs to break when the translation finishes and it conflicted with this else.
44 changes: 44 additions & 0 deletions my-code/Refactoring_Dictionaries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 6 14:58:04 2020

@author: Alt-E-K
"""
# we are first going to import the modules we need
import string
#we define the variable that contains all the possible charachters for our encoder.
char_list=list(string.printable)

#we do the first level of encoding by equating each charachter to the one 7 positions to the
# left in the list, good thing about this is that it is a self-contained function and can admit
# other lists of charachters
def first_encoding(x):
encoding_dict1 = {}
for index, item in enumerate(x):
encoding_dict1[item]=char_list[index-7]
return encoding_dict1

encode_phase1=first_encoding(char_list)

#we reduce the second level of encoding from a whole function to a disctionary comprehension.
encode_phase2 = {pair[0]:ord(encode_phase1.get(pair[0]))for pair in encode_phase1}

#to my understanding this function was too dificult for dictionary comprehension.too many ifs
def same_lenght_version(x):
encoding_dict3 = {}
for pair in x:
char=str(x.get(pair[0]))
if len(char) == 2:
new_char=char+'@'
elif len(char) == 1:
new_char=char+'*/'
else:
new_char=char
encoding_dict3[pair[0]]=new_char
return encoding_dict3

encode_phase3=same_lenght_version(encode_phase2)

#the decoded dictionary can also be made with dictionary comprehension.
decoded_dict={encode_phase3.get(pair[0]):pair[0] for pair in encode_phase3}

27 changes: 27 additions & 0 deletions my-code/Script_Encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 6 15:54:19 2020

@author: Usuario
"""
#We import the functions we are going to use.
from Encoding_Decoding_Functions import ultimate_encoder
from Encoding_Decoding_Functions import improved_decoder

#we write the now way simpler script
answer1=input('Do you wish to encode or decode?')
while answer1 != None:

if answer1 == 'encode':
ultimate_encoder()

break

elif answer1 == 'decode':
improved_decoder()
break
#don't know if the continue is in the right place because the loop that
else:
answer1=input('Please select a valid option:')
continue

Loading