Provide with either C++ or Python Code for this Problem. You will be given a lar
ID: 3815660 • Letter: P
Question
Provide with either C++ or Python Code for this Problem.
You will be given a large text file. You need to write a program which will read the file and find the frequencies of the characters in the file. Assign unique code (say, binary code) for each character. Print all characters with their frequencies and assigned code Finally replace all characters in the file by assigned characters. You must have another program to convert back to text file from assigned coded file. Efficient way to convert text file into assigned coded file. We evaluate your program by measuring the time how long your program will take to convert text file into coded file. We evaluate your program by measuring your coded file how much place it will require for keeping all information. We evaluate your program by verifying the text after converting back from coded file into text file to check whether it loss/modify any actual data.Explanation / Answer
'''Opening the existing file to read the string'''
f = open("characterset.txt","r")
'''Opening a temporary file to write the byte code output'''
f2 = open("temp_output.txt","w")
char_count_dict = {}
tmp_val = None
''' looping over the string to get character count'''
for l in f:
for i in range(len(l)):
if l[i] not in char_count_dict:
char_count_dict[l[i]] = 1
else:
char_count_dict[l[i]] += 1
'''Getting the byte code (binary code) of all the characters in the given string'''
byte_code_val = ' '.join(map(bin,bytearray(l,'utf8')))
tmp_val = byte_code_val
''' Replacing the characters with the bytecode '''
f2.write(byte_code_val)
bin_code_values = tmp_val.split(' ')
bin_code_dict = {}
for val in bin_code_values:
if val not in bin_code_dict:
bin_code_dict[val] = 1
else:
bin_code_dict[val] += 1
temp_list1 = []
temp_list2 = []
temp_list3 = []
key_match_dict = {}
for i,val in enumerate(char_count_dict):
temp_list1.append([val, char_count_dict[val]])
for i,val in enumerate(bin_code_dict):
temp_list2.append([val, bin_code_dict[val]])
for i in range(len(temp_list1)):
temp_list3.append([temp_list1[i][0], temp_list1[i][1], temp_list2[i][0]])
for i in range(len(temp_list3)):
'''Printing the Character, frequency and equivalent byte code'''
print("Character: " + temp_list3[i][0] + " Count: " + str(temp_list3[i][1]) + " ByteCode " + temp_list3[i][2] )
if temp_list3[i][0] not in key_match_dict:
key_match_dict[temp_list3[i][0]] = temp_list3[i][2]
else:
pass
f.close()
f2.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.