Page 432 - Computer Science V2.0 Class 11
P. 432

2.  Can you use the function wordCountDict(lst)that you developed in a previous question to count the number
                 of occurrences of numbers in a list.
                 Answer: Yes, because the function does not make use of the fact that the objects in the list are strings. So, it is
                 applicable to any list of objects, including heterogeneous lists. However, the objects in the list should be immutable
                 because we are using them as keys in the dictionary.
                Example:
               >>> wordCountDict(['ab', 7, (2, 3, 4), 27, 7, 'ab', (2, 3, 4), (1, 2, 3)])
                    {'ab': 2, 7: 2, (2, 3, 4): 2, 27: 1, (1, 2, 3): 1}
               >>> wordCountDict(['ab', 7, [2, 3, 4], 27, 7, 'ab', [2, 3, 4], {1:1, 2:4}])
                    Traceback (most recent call last):
                      File "<pyshell#25>", line 1, in <module>
                        wordCountDict(['ab', 7, [2, 3, 4], 27, 7, 'ab', [2, 3, 4], {1:1, 2:4}])
                      File "F:\DOWNLOADS\try1.py", line 15, in wordCountDict
                        if w in wordCount:
                    TypeError: unhashable type: 'list'
              3.  Write a function stateCapitalDict() that prompts a user to enter some states and capitals and outputs a
                 dictionary stateCapital. Make use of the function stateCapitalDict() to write a menu driven program
                 that does the  following:
                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 city's name and find the state whose capital is the city entered by the user. Display appropriate
                   messages.

                01 def stateCapitalDict():
                02     '''
                03     Input Parameter: None

                04     Return Value: stateCapital - Dictionary containing state as keys and capital as values
                05     '''
                06
                07     stateCapital = dict()
                08     state = input('Enter state:')
                09     capital = input('Enter capital:')
                10     while state != '' and capital != '':
                11         stateCapital[state] = capital
                12         state = input('Enter state:')
                13         capital = input('Enter capital:')
                14     return stateCapital
                15
                16
                17 def searchCapital(myDict, state):
                18     '''

                19     Input Parameters: myDict - Dictionary containing state as keys and capital as values
                20                      state - state for which capital is to be determined
                21
                22     Return Value:
                23          capital, if state is present as key in the dictionary
                24          updated dictionary with new entry for the state and capital, otherwise
                25     '''
                26     if state in myDict:
                27         print('Capital of', state, 'is',myDict[state])
                28     else:
                29         print('No record found for given state!')

               418   Touchpad Computer Science (Ver. 2.0)-XI
   427   428   429   430   431   432   433   434   435   436   437