Question

In: Computer Science

Write a program that uses a dictionary to assign “codes” to each letter of the alphabet....

Write a program that uses a dictionary to assign “codes” to each letter of the alphabet. For example:

     codes = {'A':'%', 'a':'9', 'B':'@', 'b':'#', etc....}

Using this example, the letter “A” would be assigned the symbol %, the letter “a” would be assigned the number 9, the letter “B” would be assigned the symbol “@” and so forth.

The program should open a specified text file, read its contents, and then use the dictionary to write an encrypted version of the file’s contents to a second file. Each character in the second file should contain the code for the corresponding character in the first file. Additionally, the order of the characters will be reversed. In other words, the encrypted version of the first character in the original file will be last in the new file, the encrypted version of the second character will be second to last in the new file, and so on. The encrypted version of the last character in the original file will be the first character in the new file.

Write a second program that opens an encrypted file and displays its decrypted contents on the screen.

Place your Python code and a screen shot of the output from both of your programs for #2 here (2 codes and 2 screen shots).

Solutions

Expert Solution

The first program is given below that open a file data_file.txt read file and write encrypted version of character into encrypted_file.txt . The last character in original file will be the first character in new file.

def read_file(codes):
#open data_file.txt
file=open("data_file.txt", "r")
#open encrypted_file.txt
file_w=open("encrypted_file.txt", "w")
  
count=0
str1=""
#read file character by character
while 1:
char = file.read(1)
#increment count by 1
count=count+1
if not char:
break
#append character into str1
str1=str1+char
#find length of str1
ch_count=len(str1)-1

#continue until ch_count not greater than equal to 0
while ch_count>=0:
#take character at position ch_count in str1
key=str1[ch_count]
#check character is in dictionary
if key in codes:
#store their code into ans
ans=codes[key]
#otherwise keep as it is
else:
ans=key
#write into encrypted_file
file_w.write(ans)
#decrement count by 1
ch_count=ch_count-1
print("Encrypted successfully")
#close file
file.close()
file_w.close()

codes={'A':'%','B':'@','C':'h','D':'!','E':'$','F':'+','G':'&','H':'*','I':'-',
'J':'/','K':'1','L':'2','M':'3','N':'4','O':'5','P':'6','Q':'7','R':'8',
'S':'g','T':'a','U':'b','V':';','W':'c','X':'d','Y':'e','Z':'f','a':'9',
'b':'#','c':'i','d':'j','e':'k','f':'l','g':'m','h':'n','i':'o','j':'p',
'k':'q','l':'r','m':'s','n':'t','o':'u','p':'v','q':'w','r':'x','s':'y',
't':'z','u':'A','v':'B','w':'C','x':'D','y':'E','z':'F'}
read_file(codes)

The screenshot of code is given below:

The data_file.txt

This is my first program
dfJh
dhjs

encrypted_file.txt (after execution of program):

ypnj
n/lj
s9xmuxv zyxol Es yo yona

Output:

The second program is given below:

that open a file encrypted_file.txt read file and write decrypted version of character into decrypted_file.txt and also print on output screen . The last character in original file will be the first character in new file.

def read_file(codes):
#open encrypted_file.txt
file=open("encrypted_file.txt", "r")
#open decrypted_file.txt
file_w=open("decrypted_file.txt", "w")
  
count=0
str1=""
str2=""
#read file character by character
while 1:
char = file.read(1)
#increment count by 1
count=count+1
if not char:
break
#append character into str1
str1=str1+char
#find length of str1
ch_count=len(str1)-1
#continue until ch_count not greater than equal to 0
while ch_count>=0:
#take character at position ch_count in str1
val=str1[ch_count]
ans=val
#check val exist in dictionary value
for key, value in codes.items():
if val == value:
#set ans equals to key
ans=key

str2=str2+ans
#write into decrypted_file.txt
file_w.write(ans)
#decrement count by 1
ch_count=ch_count-1

print("Decrypted successfully")
print("The decrypted content is:")
print(str2)
#close file
file.close()
file_w.close()
  
codes={'A':'%','B':'@','C':'h','D':'!','E':'$','F':'+','G':'&','H':'*','I':'-',
'J':'/','K':'1','L':'2','M':'3','N':'4','O':'5','P':'6','Q':'7','R':'8',
'S':'g','T':'a','U':'b','V':';','W':'c','X':'d','Y':'e','Z':'f','a':'9',
'b':'#','c':'i','d':'j','e':'k','f':'l','g':'m','h':'n','i':'o','j':'p',
'k':'q','l':'r','m':'s','n':'t','o':'u','p':'v','q':'w','r':'x','s':'y',
't':'z','u':'A','v':'B','w':'C','x':'D','y':'E','z':'F'}

read_file(codes)

The screenshot of code is given below:

encrypted_file.txt:

ypnj
n/lj
s9xmuxv zyxol Es yo yona

decrypted_file.txt(after execution of program):

This is my first program
dfJh
dhjs

Output:


Related Solutions

write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
what is the tenth letter of the standard english alphabet?
what is the tenth letter of the standard english alphabet?
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
Instructions: 1. Write a MapReduce program to find the frequency of each letter, case insensitive, in...
Instructions: 1. Write a MapReduce program to find the frequency of each letter, case insensitive, in some given input. For example, "The quick brown fox jumps over the lazy dog" as input should generate the following (letter,count) pairs: (T, 2), (H, 1), (E, 3), etc. 2. Test your program against the 3 attached input files: HadoopFile0.txt, HadoopFile1.txt, and HadoopFile2.txt. 3. The input and output must be read/written from/into HDFS. 4. Please submit only the Java source file(s) on . 5....
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
In a Java program, how could I write a program that can assign values that would...
In a Java program, how could I write a program that can assign values that would make a rock paper scissors game work? I have a program that will generate a computer response of either rock, paper, or scissors but how can I compare a user input of "rock", "paper", or "scissors" so that we can declare either the user or the computer the winner.
(c++ programming) We want to assign a letter grade based on the following scaling chart. Write...
(c++ programming) We want to assign a letter grade based on the following scaling chart. Write a program to do it Enter your score: 85 Your grade is a “B” 0-------------------60---------70-------------80-----------90-------------100 F D C B A MUST use nested if-else statements.
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT