Program gets an input at the beginning. That inputed string can contain capital letters or any other ascii letters. We don't difference between them, so we just use lower() method. Also any letters other than letters from alphabet (numbers etc.) are used as spaces between strings. Function is supposed to analyse the input, sort it and count it. Output:
{'idk': 2, 'idc': 1, 'idf': 1}
Input:
print(word_frequency("Idk, Idc, Idk, Idf"))
I tried this and It's sorting the input, but I can't find a way to separate strings. This is what I did:
def word_frequency(text):
f = {}
pendens = ""
for s in text:
if s.isalpha():
pendens += s
else:
pendens = pendens.lower()
if pendens != " ":
if f.get(pendens, -1) != -1:
f[pendens] += 1
else:
f[pendens] = 1
pendens = pendens.lower()
if pendens != " ":
if f.get(pendens, -1) != -1:
f[pendens] += 1
else:
f[pendens] = 1
return f
print(word_frequency("Idk, Idc, Idk, Idf"))
print(word_frequency("Idk,+Idc,Idk;;-;Idf"))
print(word_frequency("help me please"))
I'm trying to get better at coding so any form of help will be appreciated :)