Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Huffman Code Programming Excercise PYTHON REQUIRED Please show all clear explana

ID: 3817298 • Letter: H

Question

Huffman Code Programming Excercise PYTHON REQUIRED Please show all clear explanation using python coding only please show screen shots and please print output in the specified format in the porblem for good feeedback thanks

import sys

def Encoder(codeDict, inputStr):
#
# Please write code here
#
#
pass

# Read input
n = int(sys.stdin.readline().strip(' '))
codeDictionary = {}
for i in xrange(n):
character, code = sys.stdin.readline().strip(' ').split()
codeDictionary[character] = code
plainString = sys.stdin.readline().strip(' ')
codedString = Encoder(codeDictionary, plainString)
print codedString

[Active] Homework 10: Huffman Code In this problem, your job is to write Huffman Code and related functionalities, in three parts: Part 1 Huffman Encoding: In this part, using the given set of codes, you encode the given string (contains only small case english letters) into a sequence of 0s and 1s representingthe Huffman Coded version of it. Part 2 Huffman Decoding: In this part, using the given Huffman tree, you decode a given sequence of 0s and 1s into a string (contains only small case english letters) Part 3 Huffman Coding: In this part, you generate the Huffman Codes for the different characters in the given string (contains only small case english letters). In this part, you need to use min-heap/priority-queue. You can use the three functions imported in the starter code. Google them for guide on howto use them. Part 1 Huffman Encoding: Input Specification: Input to this part consists of two section. The first is identical to the output of part 3, followed by the second section which matches the description of the input to Part 3 Output Specification The output should be a sequence of 0s and 1s representing the encoded version of the input string based on the given codes (length is at most 8*10A5). This output is unique based on the given input Part 2 Huffman Decoding: Input Specification: Input to this part consists of two section. The first is identical to the output of part 3, followed by the second section which matches the description of the output of Part 1 Output Specification Output of this part should match the input of Part 3. This output is unique based on the given input

Explanation / Answer

clc;

clear all;

op='y';

while op == 'y'

clear all;

close all;

clc;

tot=0;

num=0;

pp=0;

filename = input('Enter name of file : ', 's');

infile = fopen(filename, 'r');

dat=input(' for the decoded part enter yes or no (y - n) =','s');

err=input(' want to see decoded part with error press yes or no (y - n) =','s');

symb1 = [1:73] ; % Data symbols

source={'e' ;'t' ;'a'; 'o'; 'i'; 'n' ;'s' ;'h'; 'r'; 'd'; 'l'; 'c'; 'u'; 'm'; 'w'; 'f'; 'g'; 'y'; 'p'; 'b'; 'v'; 'k' ;'j'; 'x'; 'q' ;'z' };

source1={'e' 't' 'a' 'o' 'i' 'n' 's' 'h' 'r' 'd' 'l' 'c' 'u' 'm' 'w' 'f' 'g' 'y' 'p' 'b' 'v' 'k''j' 'x' 'q' 'z'};

% from victor buttigieg thesis

p=[0.0817 0.0149 0.0278 0.0425 0.1270 0.0223 0.0202 0.0609 0.0697 0.0015 0.0077 0.0403 0.0241 0.0674 0.0751 0.0193 0.0010 0.0599 0.0633 0.0906 0.0276 0.0098 0.0236 0.0015 0.0197 0.0006];

pp=sort(p,'descend');

total=sum(p);

%procedure to create a huffman code

tic;

dict=huffmandict(source,pp);

dict(26:1);

[dict,avglen] = huffmandict(source,pp); % Create dictionary.

temp = dict;

for i = 1:length(temp)

temp{i,2} = num2str(temp{i,2});

temp{i,2} = horzcat(' ',temp{i,2});

end

hcode= huffmanenco(source,dict);

source=temp;

source

enc=[0];

sourcefinal=[0 0 0 0 0 0 0];

r=1;

% open the file in the read mode

% reading the text file with white spaces

data = fscanf(infile,'%c');

n=length(data);

i=1;,q=1;

for i=1:n

msg = data(i);

for q=1:26

if (char(msg)==char(source(q,1)))

enc=[enc;source(q,2)];

num=num+1;

end

end

end

enc([1])=[];

enc

ens=enc{1};

a=length(enc);

for i=2:a

ens=horzcat(ens,enc{i});

end

ens

if(dat=='y')

sourcefinal1=ens;

received_bits = sourcefinal1;

display('At the receiver end, the received bits are : ')

received_bits

display('no of character in encoded file: ')

disp(num)

display ('lenght of encoded bits are:')

l=length(received_bits);

disp(l/3)

if(err=='y')

if(received_bits(19)==0)

received_bits(19)=1;

else

received_bits(19)=0;

end

end

display('At the receiver end, the received bits are : ')

received_bits

t=0;

l=length(received_bits);

a=received_bits(1);

len=1;

c=a;

a1=':';

for i=2:length(received_bits)

w='' ;

for k=1:26

r=cell2mat(source(k,2));

if (strcmp(r,c)==1)

w=cell2mat(source(k,1));

for pp=1 : length(r)

if(r(pp)== '0' || r(pp)== '1')

tot=tot+1;

end

end

c=[];

end

end

a1=strcat(a1,w);

if(isempty(c)==1)

b=received_bits(i);

b1=num2str(b);

c=horzcat(c,b1);

else

b=received_bits(i);

b1=num2str(b);

c=horzcat(c,b1);

end

end

for k=1:26

r=cell2mat(source(k,2));

if (strcmp(r,c)==1)

w=cell2mat(source(k,1));

for pp=1 : length(r)

if(r(pp)== '0' || r(pp)== '1')

tot=tot+1;

end

end

c=[];

end

end

a1=strcat(a1,w);

a1=strcat(a1,'#');

toc

display('decoded output in decoding is: ')

disp(a1)

display('decoded bit length is :')

disp(tot)

display('no of character in encoded file: ')

disp(length(a1) - 2)

else

display('choose yes')

end

op=input('if you want to continue enter yes or no (y - n) =','s');

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote