Exercise Solutions: Dictionaries

Part A: Search a Dictionary

  1. Write a function called return_cost that takes a dictionary and flavor choice as parameters.

    1. The function searches the dictionary for the flavor and returns its cost.

    2. If the flavor is not in the dictionary, return a value of 0.

    1
    2
    3
    4
    def return_cost(menu, item):
       if item in menu:
          return menu[item]
       return 0
    

    Back to the exercises

  1. Write a function called fanciest_flavor that takes a dictionary as a parameter. The function should return the key name for the most expensive choice in the dictionary.

    1
    2
    3
    4
    5
    6
    7
    8
    def fanciest_flavor(menu):
       highest_cost = 0
       fanciest = ''
       for (flavor, price) in menu.items():
          if price > highest_cost:
                fanciest = flavor
                highest_cost = price
       return fanciest
    

    Back to the exercises

Part B: Keys from a Collection

  1. Write a function called assign_tickets that takes a list of names as a parameter.

    1
    def assign_tickets(names):
    

    Back to the exercises

  1. For the value of each key, assign a random integer from 100-500.

    tickets[name] = random.randint(100, 500)
    

    Back to the exercises

  1. In main(), call the assign_tickets function and assign the result to a ticket_holders variable.

    ticket_holders = assign_tickets(names)
    

    Back to the exercises

Part C: Modify Values

Call the function fix_tickets, and use ticket_holders as the argument. The fix_tickets function should:

  1. Accept a dictionary as a parameter.

  2. Loop through the dictionary and check each ticket number to see if it is in the range 100-199 (including the end points).

  3. For a ticket within the range, increase its value by 500 and reassign it to the key.

  4. Unless you cloned the dictionary, there is no need to return the updated collection.

1
2
3
4
def fix_tickets(tickets):
   for (key, value) in tickets.items():
      if 100 <= value <= 199:
            tickets[key] += 500

Back to the exercises

Part D: Counting Characters

Write a function called character_count that counts how many times each character appears in a string.

The function should:

  1. Accept a string as a parameter.

  2. Create an empty dictionary called counts.

  3. Loop through the string and check each character.

    1. If the character does NOT exist in as a key in counts, add it and assign it a value of 1.

    2. If the character DOES exist as a key in counts, increase its value by one.

  4. Return the completed counts dictionary and assign it to a results variable in main().

1
2
3
4
5
6
7
8
def character_count(a_string):
   counts = {}
   for char in a_string.lower():
      if char in counts:
            counts[char] += 1
      else:
            counts[char] = 1
   return counts

Back to the exercises

Part E: Use a List to Sort Key/Value Output

  1. In the main() function, loop through the results dictionary and print each key/value pair on its own line.

    1
    2
    for (key, value) in results.items():
       print(f"{key}: {value}")
    

    Back to the exercises

  1. Modify your code again, but this time display the character counts in alphabetical order.

    1. Use the list function to create a list of the keys from the results dictionary.

    2. Sort the list, then use a loop to print the key/value pairs, one pair per line.

    1
    2
    keys = list(results.keys())
    keys.sort()
    

    Back to the exercises