Skip to content

LivingXu/Computer-Mathematics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computer-Science

In this series of three programming tasks, we will implement together a program that will play optimally in a tricky dice game! Your program will be given a list of dices and will decide who chooses the dice first (you or your opponent). When the dices are chosen, we will simulate 10000 throws. Each time your number is greater, you get $1 from your opponent. Conversely, each time your number is smaller, you pay $1 to your opponent. Your ultimate goal is to implement a program that always wins in such a simulation.

  1. First Task: Compare Two Dices Implement a function that takes two dices as input and computes two values: the first value is the number of times the first dice wins (out of all possible 36 choices), the second value is the number of times the second dice wins. We say that a dice wins if the number on it is greater than the number on the other dice. To debug your implementation, use the following test cases: Sample 1 Input: dice1 = [1, 2, 3, 4, 5, 6], dice2 = [1, 2, 3, 4, 5, 6] Output: (15, 15) Sample 2 Input: dice1 = [1, 1, 6, 6, 8, 8], dice2 = [2, 2, 4, 4, 9, 9] Output: (16, 20)

Code:

def count_wins(dice1, dice2):

    assert len(dice1) == 6 and len(dice2) == 6
    dice1_wins, dice2_wins = 0, 0

    # write your code here

    return (dice1_wins, dice2_wins)
  1. Second Task: Is there the Best Dice? Now, your goal is to check whether among the three given dices there is one that is better than the remaining two dices. Implement a function that takes a list of dices and checks whether there is dice (in this list) that is better than all other dices. We say that a dice is better than another one, if it wins more frequently (that is, out of all 36 possibilities, it wins in a cases, while the second one wins in b cases, and a>b). If there is such a dice, return its (0-based) index. Otherwise, return -1. Use the following datasets for debugging: Sample 1 Input: [[1, 1, 6, 6, 8, 8], [2, 2, 4, 4, 9, 9], [3, 3, 5, 5, 7, 7]] Output: -1 Sample 2 Input: [[1, 1, 2, 4, 5, 7], [1, 2, 2, 3, 4, 7], [1, 2, 3, 4, 5, 6]] Output: 2 Sample 3 Input: [[3, 3, 3, 3, 3, 3], [6, 6, 2, 2, 2, 2], [4, 4, 4, 4, 0, 0], [5, 5, 5, 1, 1, 1]] Output: -1

Code:

def find_the_best_dice(dices):

    assert all(len(dice) == 6 for dice in dices)

    # write your code here
    # use your implementation of count_wins method if necessary

    return -1
  1. Third Task: Implement a Strategy You are now ready to play! Implement a function that takes a list of dices (possibly more than three) and returns a strategy. The strategy is a dictionary: If, after analyzing the given list of dices, you decide to choose a dice first, set strategy["choose_first"] to True and set strategy["first_dice"] to be the (0-based) index of the dice you would like to choose If you would like to be the second one to choose a dice, set strategy["choose_first"] to False. Then, specify, for each dice that your opponent may take, the dice that you would take in return. Namely, for each i from 0 to len(dices)-1, set strategy[i] to an index j of the dice that you would take if the opponent takes the i-th dice first. Use the following datasets for debugging: Sample 1 Input: [[1, 1, 4, 6, 7, 8], [2, 2, 2, 6, 7, 7], [3, 3, 3, 5, 5, 8]] Output: {'choose_first': False, 0: 1, 1: 2, 2: 0} Sample 2 Input: [[4, 4, 4, 4, 0, 0], [7, 7, 3, 3, 3, 3], [6, 6, 2, 2, 2, 2], [5, 5, 5, 1, 1, 1]] Output: {'choose_first': True, 'first_dice': 1} Note that your answers do not have to coincide with the answers above. First, the order of elements does not matter in the dictionary. Second, the dictionary might contain extra information that is not required in the statement of the problem. For example, {0: 3, 'first_dice': 1, 'choose_first': True} is also a correct output in Sample 2.

Code:

def compute_strategy(dices):

    assert all(len(dice) == 6 for dice in dices)

    strategy = dict()
    strategy["choose_first"] = True
    strategy["first_dice"] = 0
    for i in range(len(dices)):
        strategy[i] = (i + 1) % len(dices)
    
        # write your code here

    return strategy

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages