Question:
Devise an algorithm and write the Python code to count the number of unique words in a given passage.
The paragraph may contain words with special characters such as !
, ?
, .
, ,
, :
and ;
and digits are not permitted.
Special character must occur only at the end of a word in a passage that is Hello World!
is valid but Hello !World
or Hello Wor!ld
is invalid.
No two special character occur together. Print Invalid input
for such cases.
Count words without special characters. Counting must case insensitive. Print words in lower case and in sorted order.
My code:
import sys
from pprint import pprint`
import re
line=raw_input()
line.lower()
l=line.split(" ")
d=set(l)
count={}
for word in d:if word in count:count[word]+=1else:count[word]=1
pprint(count)
Expected Output:
{'are': 2, 'better': 1, 'dear': 2, 'how': 1, 'you': 2}
My Program Output:
{'Are': 1, 'How': 1, 'are': 1, 'better': 1, 'dear?': 1, 'you': 1}