Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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]+=1
    else:
        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}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
235 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to remove these 2 lines and add the line:

REMOVE:  
line.lower()   
l=line.split(" ")

ADD:
l = re.sub(r"s+[!?.,:@]+s+", r" ", s2.lower()).split(" ")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...