Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write a program that reads a list of words. Then, the program outputs those words and their frequencies (case insensitive)

if the code input is:

hey Hi Mark hi mark

the program wants the lower case number count AND for the list of words to remain upper case if they were upper case. So the correct output would be:

hey 1
Hi 2
Mark 2
hi 2
mark 2

I’ve created the correct number count and list using this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

line = input()
norm = line.split()
low = line.lower().split()
for chr in low:
    freq = low.count(chr)
    print(freq)
for x in norm:
    print(x)

the output of this is:

1
2
2
2
2
hey
Hi
Mark
hi
mark

I thought I could use print(x,freq) in my last for loop to give the correct out put but it creates an additional hey for some ungodly reason and this becomes the output:

hey 2
Hi 2
Mark 2
hi 2
mark 2

I have no idea where it is getting the extra hey.
is there anyway to combine the print results from 2 for loops to create a correct output? Or do you know where the extra ‘hey’ is coming from?

>Solution :

You can iterate on both original and lower versions at once using zip

for word, word_lower in zip(norm, low):
    print(word, low.count(word_lower))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading