Solutions: You can find the file with solutions for all questions here.

## Quick Review on Dictionary

### Question 1: Merge Dictionaries

Implement the function `merge_dict`. The `merge_dict` function merges two dictionaries with the same keys together.

``````def merge_dict(d1, d2):
"""Returns a dictionary with two dictionaries merged together. You can assume that the same keys appear in both dictionaries.

>>> cs61a = {"midterms":2, "projects":3}
>>> cs61b = {"midterms":2, "projects":4}
>>> combined = merge_dict(cs61a, cs61b)
>>> combined
{'midterms': 4, 'projects': 7}
"""
result_dict = {}
for work in d1:
result_dict[work] = d1[work] + d2[work]
return result_dict``````

Use OK to test your code:

``python3 ok -q merge_dict --local``

## Mutation

### Question 2: Merge Dictionaries with Mutation

Now let's build the same function, but this time, instead of creating a new dictionary, modify one of the dictionary to become the combination of both dictionaries under `+`.

``````def merge_dict_mutate(d1, d2):
"""Write a function that merge the second dictionary into the first dictionary. You can assume
that the same keys appear in both dictionaries.

>>> bank = {"Annie":1000, "David":500}
>>> new_deposits = {"Annie":700, "David":800}
>>> merge_dict_mutate(bank, new_deposits)
>>> bank
{'Annie': 1700, 'David': 1300}
"""
for money in d1:
d1[money] = d1[money] + d2[money]``````

Use OK to test your code:

``python3 ok -q merge_dict_mutate --local``

### Question 3: Combine List with Mutation

Build the function, `list_combine`, that takes in one list and modify the list so that it is left with one item that's the `+` combination of every single item in the list. Hint: You may find the method `lst.pop()` useful. Hint: Use `+` to combine items in the list.

``````>>> a = [1,4,5,6,7,8]
>>> a.pop() # If no argument is put in, the last item in the list is popped off.
8 # The pop method returns the item popped off (aka taken off the list)

>>> a [1, 4, 5, 6, 7]
>>>a.pop (2) # Pop off the third item in the list.
5

>>> a
[1, 4, 6, 7]``````
``````def list_combine(lst):
"""Write a function that combines all the items in the list into one item and put it as the only item in the list.

>>> pokemon = [4, 5, 3, 2, 1, 6]
>>> list_combine(pokemon)
>>> pokemon
[21]
>>> alphabet = ["a", "b", "c", "d", "e"]
>>> list_combine(alphabet)
>>> alphabet
['abcde']
"""
for i in range(1, len(lst)):
lst[0] = lst[0] + lst[i]
for i in range(1, len(lst)):
lst.pop()
``````

Use OK to test your code:

``python3 ok -q list_combine --local``

### Question 4: Dictionary Cycle

Build a function that takes in a dictionary. Each of the keys in the dictionary is mapped to a list. Build the function such that the dictionary is modified so that the key becomes the last item in the list, and the first item in the list becomes the new key. Hint: You may find the method `dict.pop()` useful. Works similarly to `lst.pop()`!

``````def dict_cycle(dictionary):
"""Write a function that cycles each of the key-value pair such that the key becomes the last
item in the value list, and the first item of the list becomes the new key.

>>> hamster = {"a":["b","c","d"], "w":["x","y","z"]}
>>> dict_cycle(hamster)
>>> hamster
{'b': ['c', 'd', 'a'], 'x': ['y', 'z', 'w']}
"""
keys = list(dictionary.keys())
for key in keys:
val = dictionary.pop(key)
dictionary[val[0]] = val[1:] + [key]``````

Use OK to test your code:

``python3 ok -q dict_cycle --local``

### Question 5: Pokemon

Remember the good old days when we played Pokemon Go? Complete the implementation of a Pokemon ADT below.

``````def make_gym(a, b, c, d):
"""Returns a pokemon gym (represented by list) of the four pokemons a, b, c, d."""
return [a, b, c, d]

def gym_size(gym):
"""Returns the size of the gym."""
return len(gym)

def make_pokemon_set():
"""Returns a dictionary of pokemon methods.

>>> my_pokemons = make_pokemon_set()
>>> my_pokemons["evolve"]("charmander")
'charizard'
>>> my_pokemons["evolve"]("celebi")
'celebi'
>>> my_gym = make_gym("charmander", "celebi", "pikachu", "rattata")
>>> my_pokemons["evolve_all"](my_gym)
>>> my_gym
['charizard', 'celebi', 'raichu', 'raticate']

"""

pokemons = {"charmander":"charmeleon",
"charmeleon":"charizard",
"squirtle":"wartortle",
"wartortle":"blastoise",
"rattata":"raticate",
"sandshrew":"sandslash"}

def evolve(pokemon):
return _evolve(pokemons, pokemon)

def evolve_all(gym):
_evolve_all(pokemons, gym)

"""Takes in a pokemon and the form it evolves to and adds it to the pokemon
dictionary set.
"""
pokemon_set[pokemon] = evolution

def _evolve(pokemon_set, pokemon):
"""Takes in a pokemon and returns its final evolved form. Use the pokemon_set to check for
what the pokemon should evolve to. If the pokemon is not in the pokemon set, keeps its
status as is.

"""
if pokemon not in pokemon_set:
return pokemon
else:
return _evolve(pokemon_set, pokemon_set[pokemon])

def _evolve_all(pokemon_set, gym):
"""Takes in a gym and evolve all the pokemons in the gym. You should be modifying the gym,
not returning a new gym. Use the evolve function you've defined above!

"""
for i in range(gym_size(gym)):
gym[i] = _evolve(pokemon_set, gym[i])  ``````

Use OK to test your code:

``python3 ok -q make_pokemon_set --local``