[Help]H.W issues
-
import re
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def creation_of_dictionary(message):
print(f"In creation_of_dictionary: message= {message}")
m = {}
l = []
z4 = {}
mc = ['r', 'o', 't', 'e']
for i in message:
if i.isalpha():
b = i.lower()
l.append(b)
print(f"creation_of_dictionary:l={l}")for g in range(len(l)): f = l.count(l\[g\]) m\[l\[g\]\] = f tuples = m.items() tuples = sorted(tuples, key=lambda tuples: tuples\[1\]) print(f"tuples = {tuples}") for e in \[-4, -3, -2, -1\]: z4\[mc\[m\]\] = tuples\[e\]\[0\] z4\[tuples\[e\]\[0\]\] = mc\[e\] print(z4) return (z4)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 2
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def decode(message, dict):
lists = []
for m in message:
l = m.lower()
lists.append(l)
for s in range(len(lists)):
if lists[s] in dict.keys():
lists[s] = dict[lists[s]]
b = ''.join(lists)
return b----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def txt_decode(path):
lines = []
newdic = {}
m = open(path, 'r')
q = m.readlines()
m.close()
for i in range(len(q)):
q[i] = q[i].rstrip()
newdic = creation_of_dictionary(''.join(q))
for t in range(len(q)):
lines.append(decode(q[t], newdic))
f = open(path, 'a')
f2 = open("result.txt", 'w')
f.write("\n The encryption for the above text is:\n")
for n in range(len(lines)):
f.write(lines[n])
f2.write(lines[n])
f.write('\n')
f2.write('\n') -
import re
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def creation_of_dictionary(message):
print(f"In creation_of_dictionary: message= {message}")
m = {}
l = []
z4 = {}
mc = ['r', 'o', 't', 'e']
for i in message:
if i.isalpha():
b = i.lower()
l.append(b)
print(f"creation_of_dictionary:l={l}")for g in range(len(l)): f = l.count(l\[g\]) m\[l\[g\]\] = f tuples = m.items() tuples = sorted(tuples, key=lambda tuples: tuples\[1\]) print(f"tuples = {tuples}") for e in \[-4, -3, -2, -1\]: z4\[mc\[m\]\] = tuples\[e\]\[0\] z4\[tuples\[e\]\[0\]\] = mc\[e\] print(z4) return (z4)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 2
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def decode(message, dict):
lists = []
for m in message:
l = m.lower()
lists.append(l)
for s in range(len(lists)):
if lists[s] in dict.keys():
lists[s] = dict[lists[s]]
b = ''.join(lists)
return b----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Part 3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
def txt_decode(path):
lines = []
newdic = {}
m = open(path, 'r')
q = m.readlines()
m.close()
for i in range(len(q)):
q[i] = q[i].rstrip()
newdic = creation_of_dictionary(''.join(q))
for t in range(len(q)):
lines.append(decode(q[t], newdic))
f = open(path, 'a')
f2 = open("result.txt", 'w')
f.write("\n The encryption for the above text is:\n")
for n in range(len(lines)):
f.write(lines[n])
f2.write(lines[n])
f.write('\n')
f2.write('\n')אנטון מוטרוק 2021 wrote:
errors such as message.txt file doesnt exist etc
If you get errors then you need to provide the exact text, and explain which line they occurred on. Remember we have never seen this code before, have no idea what is is supposed to do or what data it is processing. Also, instead of trying to write the entire application in one go, stop and look at the task(s) you have been set. Go by the numbers in your questions: write the code just for that step, and test it until it works successfully. Only then should you move on to the next step, and so on, until you complete the entire application.