In: Computer Science
EncryptedA.txt:
vcbdwvcbwqarr, c xdwzavwqarr u juwqx uwrr cjcm vcbdgzum bkxgmq. yw rrcbtjaqurr qazamgcr u'x wml iwgxgcmb bkxgmq, za'm yykxygujg'x dam vcw fayxgygcrkxum am qcjjk qlmwkr. am azbur ax cgj xaywj tar, xuwrr am qlwgbdgu av vdcxf tjwbfdjwa, a qcmujycm txarwgmgq cx qaywx buxxg'x furck c rrwymarrglar cx qaywx faycbdxwtk vgjlxuj. raywgzgurr zclj bwfdmwq u ruxxg furck cjvcwmgq, qcm qammlaz iwgxgcmb u'x wml a tuvtw c uwrr am fcmyur rwlgzgcrck iwgxgcmb wmgqvc. cx uj a xdaywj, tk'm qlwgbdgu am a jctuxra yygzwquj fwmwrjcwbduj, cf amc av vdxgyazquj vcmfwgmgum jjw'x uwrr am qlwgbdgu cx ywrrcjlwrr cx qaywx vcxf g vcmfwgmgum, km u'x qlgx-qayxgygcrkxum fambcy am a tar. yw'g qcylar am wkuq u "lwgbdxwruwrr cmlwrrkz raqm" lwrg grru qaycrrwy fcwj iwxbdamcz xdalguj qarc ram av vcmfwgmgum. yw'g xdurrlar cx txuygcmmcwbd ixcly c quxyurlar g rrwxtam bxgmgcwbd duxvumcgrr. rcxicxurr a ixgy lwgmgruq quxrum txulm avrrgdwkxgcr zlarruquj u yjamarruwrr cx uj vcxlujcwbd bkxgmq, cv a vurr "qlcxbdkz" a fcyurr wg rxgm. ralwrurr txulm wg yur am "damur yjgm" cv a rxgmgcwbd c qcyurr bkxgmq, lwrg grru rrwxtam rwgzwt am quyam cv avrrgdwkxgcr. tk ycxl cx uj tlabc cycj qarc facmgr amrru. batgc jclwx vcg dkmcmjcrrgcr aruwrr.
I am making a dictionary list of alphabets that counts the number of letters. I almost have the code correct however, I am not able to ignore punctuation and give a value of zero to a letter that does not appear in the text
Example of correct code:
a: 78
b : 31
c,88
d,28
e,0
f,18
etc
import string
encryptedA = open("encryptedA.txt")
encryptedA = encryptedA.read()
def abcfreq(lettervalues):
freq = {}
for j in lettervalues:
if j in string.punctuation:
pass
elif j in freq:
freq[j] += 1
else:
freq[j] = 1
for key, value in sorted(freq.items()):
print("%s,%d" % (key, value))
x = abcfreq(encryptedA)
print(x)
Changed Code in python
import string
encryptedA = open("encryptedA.txt")
encryptedA = encryptedA.read()
def abcfreq(lettervalues):
freq = {}
#lets add all the smlaa letters with count 0 in our dictionary
for x in string.ascii_lowercase:
freq[x]=0
#If the character exists in dictionary then only we will increment the count
for j in lettervalues:
if j in freq:
freq[j] += 1
# we dont even need to sort now
for key, value in freq.items():
print("%s,%d" % (key, value))
x = abcfreq(encryptedA)
New Output
a,78
b,31
c,88
d,28
e,0
f,18
g,78
h,0
i,7
j,36
k,22
l,32
m,76
n,0
o,0
p,0
q,41
r,114
s,0
t,19
u,70
v,27
w,76
x,72
y,40
z,16
Explanation
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.