So I wrote this code that takes in a file, filename: str and returns the number of times each letter exists in the string in the form of ‘+’ .. here is my code
def letterhelper(filename):
r = list(filename)
c_r = set(r)
c_r.remove(' ')
c_r.remove(',')
c_r.remove('.')
c_r.remove('\n')
f = []
for x in c_r:
f.append([-r.count(x), x])
return f
def charHistogram(data: str):
r = open(filename)
q = r.read()
g = letterhelper(str.lower(q))
for t in sorted(g):
print(t[1], (-t[0]) * '+')
and data is a separate file which will be opened by function letterhelper()
A sample input that data may contain is…
"My Brothers and Sisters give me stress"
So the issue is, when data is
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Praesent ac sem lorem. Integer elementum
ultrices purus, sit amet malesuada tortor
pharetra ac. Vestibulum sapien nibh, dapibus
nec bibendum sit amet, sodales id justo.
the function correctly returns
e ++++++++++++++++++++++++
t ++++++++++++++++++
s +++++++++++++++++
i ++++++++++++++++
a +++++++++++++++
m ++++++++++++
r ++++++++++++
u ++++++++++++
l +++++++++
n +++++++++
o +++++++++
c +++++++
d +++++++
p +++++++
b +++++
g ++
h ++
j +
v +
None
but if data = Someday Imma be greater than the rest
the output is
c_r.remove(',')
KeyError: ','
What changes should I make so that my code correctly returns a histogram like when data is "Lorem ipsum ….." for all string inputs provided??
>Solution :
def letterhelper(filename):
r = list(filename)
c_r = set(r)
chars_to_remove = (' ', ',', '.', '\n')
for char in chars_to_remove:
if char in c_r:
c_r.remove(char)
f = []
for x in c_r:
f.append([-r.count(x), x])
return f
This would solve the problem and make is easier for you to add more characters for removal in future.