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
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.