Dictionaries Worksheet
Problem 1
Write a function highest_value that accepts a dictionary as its argument. The dictionary will have postive integer or float values. The function should return the key of the highest value. Assume the highest value is unique and that the dictionary has at least one entry.
Examples: highest_value({1: 34, 2: 51, 3: 50}) returns: 2 highest_value({'a': 2, 'b': 1, 'c': 5}) returns: 'c'
# Your code hereassert highest_value({"hello": 5, 12: 50, True: 51}) == True
assert highest_value({'a': 2, 'b': 1.5, 'c': 4.5, 'd': 5}) == 'd'Problem 2
Write a function count_lengths that takes as argument a list of strings, and returns a dict: the keys of the dict will be lengths of strings, and the values will be how often those lengths appear.
Examples:
count_lengths(["A", "B", "AB", "ABC"])returns dict{1: 2, 2: 1, 3: 1}count_lengths(["A", "B", "AB", "ABC", "BC", "ABC", "D"])returns dict{1: 3, 2: 2, 3: 2}
# Your code hereassert count_lengths(["A", "B"]) == {1: 2}
assert count_lengths(["a", "aa", "a"]) == {1: 2, 2: 1}
assert count_lengths(["aa", "aa", "aa"]) == {2: 3}
assert count_lengths(["a", "aa", "a", "abcde"]) == {1: 2, 2: 1, 5: 1}
assert count_lengths(["a", "aa", "a", "abcde", "abc"]) == {1: 2, 2: 1, 3:1, 5: 1}Problem 3
Write a function locator that takes one argument, a string, and returns a dictionary. The keys of the dictionary will the characters of the string, and the values will be the index of the first occurrence of each character.
Examples:
locator("many")returns dict{'m': 0, 'a': 1, 'n': 2, 'y': 3}locator("loop")returns dict{'l': 0, 'o': 1, 'p': 3}
# Your code hereassert locator("beep") == {'b': 0, 'e': 1, 'p': 3}
assert locator("fever") == {'f': 0, 'e': 1, 'v': 2, 'r': 4}
assert locator("deliver") == {'d': 0, 'e': 1, 'l': 2, 'i': 3, 'v': 4, 'r': 6}Problem 4
Write a function vowel_freq that takes a string argument and returns a dictionary. The elements of the dictionary are vowels and how many times they’re in the input string. The dictionary should not be case sensitive
Example: vowel_freq("hEllo WOrld") returns: {'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}
# Your code hereassert vowel_freq("hEllo WOrld") == {'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}
assert vowel_freq("aeiouAEIOU") == {'a': 2, 'e': 2, 'i': 2, 'o': 2, 'u': 2}
assert vowel_freq("") == {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}Problem 5
Write another function locate_all that takes one argument, a string, and returns a dictionary. The keys of the dictionary will the characters of the string, and the values will be the index of the character’s location (if the character appears once), or a list of all character locations (if the character appears more than once).
Examples:
locate_all("many")returns dict{'m': 0, 'a': 1, 'n': 2, 'y': 3}locate_all("loop")returns dict{'l': 0, 'o': [1, 2], 'p': 3}locate_all("meet them")returns dict{'m': [0, 8], 'e': [1, 2, 7], 't': [3, 5], ' ': 4, 'h': 6}
# Your code hereassert locate_all("many") == {'m': 0, 'a': 1, 'n': 2, 'y': 3}
assert locate_all("fever") == {'f': 0, 'e': [1, 3], 'v': 2, 'r': 4}
assert locate_all("everything is more difficult these days") == {'e': [0, 2, 17, 31, 33],
'v': 1,
'r': [3, 16],
'y': [4, 37],
't': [5, 27, 29],
'h': [6, 30],
'i': [7, 11, 20, 23],
'n': 8,
'g': 9,
' ': [10, 13, 18, 28, 34],
's': [12, 32, 38],
'm': 14,
'o': 15,
'd': [19, 35],
'f': [21, 22],
'c': 24,
'u': 25,
'l': 26,
'a': 36}Problem 6
Write a function key_value_switch that accepts an argument that is a dictionary. The function should return a new dictionary that contains the original dictionary’s entries switched (i.e. the values of the original dictionary are the keys of the output dictionary). For any duplicate values in the original dictionary, they should be added to the new dictionary as a list.
Examples: key_value_switch({'a': 1, 'b': 2, 'c': 3, 'd': 2}) returns: {1: 'a', 2: ['b', 'd'], 3: 'c'}
# Your code hereassert key_value_switch({'a': 1, 'b': 2, 'c': 3, 'd': 2}) == {1: 'a', 2: ['b', 'd'], 3: 'c'}
assert key_value_switch({}) == {}
assert key_value_switch({1: 2, 2: 2, 3: 2}) == {2: [1, 2, 3]}
assert key_value_switch({'mango': 5, 'animal': 6, 'python': 6, 'Petra': 5, 'Joe': 3, 'Vik': 3}) == {5: ['mango', 'Petra'], 6: ['animal', 'python'], 3: ['Joe', 'Vik']}