Sunday, December 13, 2020

The Joy of Computing Using Python December 13 Programming test - Session 2 (8PM to 9PM) Programming Exam 2: Word Count Solution

 

Programming Exam 2: Word Count

Due on 2020-12-13, 21:00 IST
You are given some words. Some of them may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification.

Note: 
All the words are composed of lowercase English letters only.

Input Format
It should contain all the words separated by space.

Output Format
Output the number of distinct words from the input  
Output the number of occurrences for each distinct word according to their appearance in the input.
Both the outputs should be separated by space.

Sample Input
bcdef abcdefg bcde bcdef

Sample Output
3 211

Explanation
There are distinct words. Here, "bcdef" appears twice in the input at the first and last positions. The other words appear once each. The order of the first appearances are "bcdef", "abcdefg" and "bcde" which corresponds to the output.

Your last recorded submission was on 2020-12-13, 20:09 IST
Select the Language for this assignment. 
1
li=input().split()
2
d=dict()
3
for items in li:
4
  d[items]=d.get(items,0)+1
5
print(len(d),end=' ')
6
for values in d.values():
7
  print(values,end='')

No comments:

Post a Comment