Due by 10/26/2018

Instructions

Download hw08.zip. Inside the archive, you will find starter files for the questions in this homework, along with a copy of the OK autograder.

Submission: When you are done, submit the homework by uploading the hw08.py file to okpy.org. You may submit more than once before the deadline; only the final submission will be scored.

Readings: This homework relies on following references:

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}
    """
    "*** YOUR CODE HERE ***"
    return ______

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}
    """
    "*** YOUR CODE HERE ***"
    "SOLUTION HERE"

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']
    """
    "*** YOUR CODE HERE ***"
    

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']}
    """
    "*** YOUR CODE HERE ***"
    

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["add"]("pikachu", "raichu")
    >>> 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 add(pokemon, evolution):
        "*** YOUR CODE HERE ***"
        

    def evolve(pokemon):
        "*** YOUR CODE HERE ***"
        return "SOLUTION HERE"

    def evolve_all(gym):
        "*** YOUR CODE HERE ***"
        

    return {"add":add, "evolve":evolve, "evolve_all":evolve_all}

def _add_pokemon(pokemon_set, pokemon, evolution):
    """Takes in a pokemon and the form it evolves to and adds it to the pokemon
    dictionary set. 
    """
    "*** YOUR CODE HERE ***"
    

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. 

    """
    "*** YOUR CODE HERE ***"
    

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!

    """
    "*** YOUR CODE HERE ***"
    

Use OK to test your code:

python3 ok -q make_pokemon_set --local