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...
Write a spell checking program (java) which uses a dictionary of words (input by the user...
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words. Functional requirements: For each word in the input string, your program should search the dictionary string for the given word. If the word is not in...
In international Morse code, each letter in the alphabet is symbolized by a series of dots...
In international Morse code, each letter in the alphabet is symbolized by a series of dots and dashes: the letter “a” for example is encoded as “×- ” while the most common letter “e” has the shortest code “×” (just a dot). What is the minimum number of dots and/or dashes needed to represent any letter in the English alphabet (26 letters)?
Encoding a message starts with assigning each letter of the alphabet with a positive integer using...
Encoding a message starts with assigning each letter of the alphabet with a positive integer using a specific pattern, thus rewriting the original message as a list of numbers instead of words. Decoding a message is the process that “undoes” the encoding process. Assume that the table below was used to encode an important secret message. A – 1 F – 6 K – 11 P – 16 U – 21 Z – 26 B – 2 G – 7...
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,...
Write a program that translates a letter grade into a number grade. Letter grades are A,...
Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D, and F, possibly followed by + or -. Their numeric values are 4, 3, 2, 1 and 0. There is no F+ or F-. A “+” increases the numeric value by 0.3, a “–“ decreases it by 0.3. However, an A+ has value 4.0.4 pts Enter a Letter grade: B- The numeric value is 2.7 Your code with comments A screenshot...
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....
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase,...
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Please use a Queue Class and Stack class.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT