Page 217 - Information_Practice_Fliipbook_Class11
P. 217

y
             >>> Enter synonyms as a tuple('angry', 'wild', 'stormy')
                 Do you want synonyms of a word? Reply Y/y of N/ny
             >>> Enter a word:irate
                 No synonyms found
                 Want to add irate  to thesaurus? Reply Y/y of N/n
                 y
             >>> Enter synonyms as a tuple('boiling', 'furious', 'heated')
                 Do you want synonyms of a word? Reply Y/y of N/ny
             >>> Enter a word:furious
                 synonyms of  furious : {'stormy', 'angry', 'wild', 'heated', 'irate', 'furious', 'boiling'}
                 Do you want synonyms of a word? Reply Y/y of N/ny
             >>> Enter a word:heated
                 synonyms of  heated : {'heated', 'irate', 'furious', 'boiling'}
                 Do you want synonyms of a word? Reply Y/y of N/nn
            More Solved Programs

            1.  Write a program to find the number of occurrences of each word in a list of words in the form of a dictionary.
              01 '''
              02 Objective: To find the frequency of words in a list.
              03 Input: list containing words
              04 Output: Dictionary of words and their frequencies
              05
              06 Approach:
              07 For each word w in the list
              08    if w is in the dictionary, increment its count
              09    else add w to the dictionary with 1 as its count
              10 '''
              11
              12 lst = eval(input('Enter the list: '))
              13
              14 wordCountDict = dict()
              15 for w in lst:
              16     if w in wordCountDict:
              17         wordCountDict[w] += 1
              18     else:
              19         wordCountDict[w] = 1
              20
              21 print('Dictionary of word count:: ', wordCountDict)
            Sample output:
             >>> Enter the list: ['Tarun','Abhigya','Tanuj','Hetansh','Tarun','Hetansh','Tanuj']
                 Dictionary of word count::  {'Tarun': 2, 'Abhigya': 1, 'Tanuj': 2, 'Hetansh': 2}
            2.  Write a program that prompts a user to:

                a.  Create a dictionary of state capitals
                b.   Accept from the user the name of the state, and find its capital. If the name of a state does not appear in the
                   dictionary, ask the user, 'Would you like to add the corresponding capital'. If yes, accept state:capital pair and
                   add to the dictionary.
                c.  Accept from a user a capital's name, and find its state.
              01 '''
              02 Objective:
              03        (1) To create a dictionary containing state as keys and
              04            capital as values
              05        (2) Searches for a capital for the user entered state. If the
              06            entered state is not present in the dictionary, the user


                                                                                             Python Dictionaries  203
   212   213   214   215   216   217   218   219   220   221   222