# Homework 7

*Due at 11:59:59 pm on Thursday, 10/31/2019.*

## Instructions

Download hw07.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 with `python3 ok --submit`

. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.

**Readings:** This homework relies on following references:

## Dictionaries

### Question 1: Flip Dictionary

Implement the function `flip_dict`

which takes in a dictionary and returns a similar dictionary where the values have become the keys and the keys have become the values.

In this problem, you can assume that each key value pair is unique, and no key or value will be repeated or used more than once.

```
def flip_dict(dictionary):
"""Returns a flipped version of the original dictionary.
>>> TAs = {"12pm-2pm": "brian", "10am-12pm": "sophia", "2pm-4pm": "alec"}
>>> flipped_TAs = flip_dict(TAs)
>>> sorted_keys = sorted(flipped_TAs)
>>> sorted_keys
['alec', 'brian', 'sophia']
>>> [flipped_TAs[i] for i in sorted_keys]
['2pm-4pm', '12pm-2pm', '10am-12pm']
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q flip_dict`

### Question 2: 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.
>>> data8 = {"midterms":1, "projects":3}
>>> data100 = {"midterms":2, "projects":3}
>>> combined = merge_dict(data8, data100)
>>> combined
{'midterms': 3, 'projects': 6}
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q merge_dict`

## Dice Rolling

**Introduction.** Alex and Srinath are playing a dice game. This dice game is special in that players choose the range of one of their dice, as well as one of their opponents. Unfortunately, both Alex and Srinath forgot to bring dice to their game - all they have is their laptop, and they need you to build an abstract object to simulate a die. Given the smallest and largest values of the die, you can construct the die using the dice constructor.

### Question 3

In order to create the dice object, create a list of all the values that the dice can take on.

```
import random
random.seed(42)
def dice(a, b):
"""Construct a die that is a list from a to b inclusive.
>>> dice(1, 6)
[1, 2, 3, 4, 5, 6]
>>> dice(3, 5)
[3, 4, 5]
>>> dice(5, 5)
[5]
"""
"*** YOUR CODE HERE ***"
def smallest(die):
"""Return the lowest value die can take on."""
return min(die)
def largest(die):
"""Return the largest value die can take on."""
return max(die)
def str_dice(die):
"""Return a string representation of die.
>>> str_dice(dice(1, 6))
'die takes on values from 1 to 6'
"""
return 'die takes on values from {0} to {1}'.format(smallest(die), largest(die))
```

Use OK to test your code:

`python3 ok -q dice`

### Question 4

Die rolls are inherently random - before rolling, we do not know what value the die will take on. We need to implement this feature in order to play the game. Python has a nice library that allows us to generate random numbers. You can read more about it here. We will be specifically using `random.choice(seq)`

, which generates a random integer from a nonempty sequence `seq`

.

```
def roll_dice(die, x):
"""Roll the die x times and return an array of the rolled values.
>>> roll_dice(dice(5, 5), 4)
[5, 5, 5, 5]
>>> max(roll_dice(dice(1, 6), 100))
6
>>> min(roll_dice(dice(1, 6), 100))
1
>>> x = sum(roll_dice(dice(1, 6), 100))/100 # Finds the mean of 100 dice rolls
>>> 3 <= x <= 4 # Checks if the mean is between 3 and 4
True
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q roll_dice`

### Question 5

Rolling a six is unfortunate in this game. Neither player wants a 6, yet they cannot avoid it. Figure out how many rolls it takes until a player rolls a 6 with a certain die.

```
def rolls_until_six(die):
"""Roll the die until you get a 6 and return the number of rolls it took to do so.
If six is not a the possible values to roll, return a string saying '6 is not a possible value of this die'
>>> rolls_until_six(dice(1, 5))
'6 is not a possible value of this die'
>>> rolls_until_six(dice(6, 6)) # Takes one roll to get 6
1
>>> x = sum([rolls_until_six(dice(1, 6)) for _ in range(100)])/100 # Repeat 100 times and average
>>> 5 <= x <= 7 # Check that it takes between 5 and 7 rolls overall on average
True
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q rolls_until_six`

### Question 6

The game has a new complication - instead of rolling a single die at a time, players are required to roll multiple dice at a time. Players start off with 2 dice in a cup, but they can add more.

```
def cup(die1, die2):
"""Construct a cup that contains die1 and die2.
>>> cup(dice(1, 1), dice(1, 2))
[[1], [1, 2]]
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q cup`

### Question 7

Add a die to your cup implementation!

```
def add_to_cup(cup, die):
"""Add die to cup.
>>> cup1 = cup(dice(1, 1), dice(1, 2))
>>> add_to_cup(cup1, dice(1, 3))
[[1], [1, 2], [1, 2, 3]]
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q add_to_cup`

### Question 8

When you roll a cup, you roll each dice in the cup once, then return an array of the rolled values.

```
def roll_cup(cup):
"""Roll every die in the cup and return an array of the rolled values.
>>> roll_cup(cup(dice(1, 1), dice(2, 2)))
[1, 2]
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q roll_cup`