Skillrack Inlab Problem 5

Solutions for Skillrack CSE1001 Inlab 5

11-Oct-2016 to 16-Oct-2016

Total time : 20 mins

Challenges : 1

Question (Word Frequency)

Given a sentence, write a program to compute the frequency of occurrence of the words in the sentence. Output should contain the word and frequency of occurrence of the word in the given sentence separated by a tab in ascending order of the word. Make the comparison to be case insensitive. For example, when the string is ‘New to Python and choosing between different versions of Python’, the output should be:

and 1

between 1

choosing 1

different 1

new 1

of 1

python 2

to 1

versions 1

Input Format:

A line of text with spaces

Output

Words in the text in the sorted order with the frequency count and separated by a tab

Word1 and its count

Word2 and its count

Word3 and its count

Solution

w=str(input())
l=w.split()
t=[]
d={}
m1=[]
m2=[]
for i in l :
    k=''
    for j in i :
        k+=j.lower()
    t.append(k)
for i in t :
    if i.isalpha() :
        if i in d :
            d[i]+=1
        else :
            d[i]=1
for i in d :
	m1.append(i)
	m2.append(d[i])
for i in range(len(m1)) :
	for j in range(1,len(m1)) :
		if m1[j-1]>m1[j] :
			m1[j-1],m1[j]=m1[j],m1[j-1]
			m2[j-1],m2[j]=m2[j],m2[j-1]
for i in range(len(m1)) :
	print(str(m1[i])+"\t"+str(m2[i]))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s