Problem Set 5(Count Words)

Count Words
Write 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 be case insensitive. Print words in lower case and in sorted order. For example, given a passage 'Programming is a skill in demand! Your programming skills are bankable assets.' The output should be
{'a': 1,
'are': 1,
'assets': 1,
'bankable': 1,
'demand': 1,
'in': 1,
'is': 1,
'programming': 2,
'skill': 1,
'skills': 1,
'your': 1}
[Hint: use pprint function for printing dictionary in sorted order.
Syntax for pprint is
pprint(dictionary name)
Include the line “from pprint import pprint” in the top of your program]
Input format:
enter a paragraph
Output format:
Dictionary of words in the passage and their count
Boundary conditions:
One paragraph is entered at a strech
Input:
para the paragraph

Processing:
from pprint import pprint
para = input()
para = para.split(' ')
words = {}
flag = True
for word in para:
    word = word.lower()
    if word.isalpha() == False:
        if word[-1].isdigit() == True:
            flag = False
            break
        word = word[0:len(word)-1]
        if word.isalpha() == False:
            flag = False
            break
    words.setdefault(word,0)
    words[word] += 1

Output:
Display words dictionary in sorted order.

Algorithm:
Step1. Import pprint function from the pprint module
Step2. Get the paragraph as the input
Step3. Split the paragraph into words
Step4. Initialize words as an empty dictionary and flag as True
Step5. Let word iterate through the paragraph
Step5.1. convert all the letters of word into lower characters.
Step5.2. if all the letters of the word are not alphabets then check if the last character is a special character if yes then remove the last character else assign flag as False and break out from the loop.
Step5.3. assign the key of words dictionary and the assign the value of the key as zero.
Step5.4. increment the key of words dictionary by 1
Step6. If flag is equal to True then display the words dictionary in sorted order.
Step7. If flag is equal to False the display invalid input.

Step8. End

Program:
from pprint import pprint
para = input()
para = para.split(' ')
words = {}
flag = True
for word in para:
    word = word.lower()
    if word.isalpha() == False:
        if word[-1].isdigit() == True:
            flag = False
            break
        word = word[0:len(word)-1]
        if word.isalpha() == False:
            flag = False
            break
    words.setdefault(word,0)
    words[word] += 1
if flag == True:
    pprint(words)
else:

    print('Invalid input')

6 comments:

  1. Replies
    1. i wouldn't be posting flowcharts from now onwards! because our teacher has asked us not to attach flowcharts...if there is a lot of demands for flowcharts then i would make them post it

      Delete
  2. this program isnt working
    Input:
    How are you dear? Are you better dear?
    Expected Output:
    {'are': 2, 'better': 1, 'dear': 2, 'how': 1, 'you': 2}
    Your Program Output:
    'Invalid input'

    ReplyDelete
    Replies
    1. When did you try to run the code?...i recently updated it and it runs flawlessly for me
      I will attach a screenshot regarding the same now..

      Delete
  3. Sexy code����

    ReplyDelete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...