Question: # This code template is created for Challenge 2: Dice Game. # The template must be complete import random as rand import string import os

# This code template is created for Challenge 2: Dice Game. # The template must be complete

import random as rand import string import os

# global variables # todo 1: std_number must have the correct student number std_number = '0000000'

# game parameters. Do Not change these variables. player_one = 'peter' player_two = 'colin' test_cases = [ {player_one: {'sides':4,'num':9} , player_two : {'sides':6,'num':6}} , {player_one: {'sides':4,'num':3} , player_two : {'sides':9,'num':2}} , {player_one: {'sides':8,'num':3} , player_two : {'sides':14,'num':1}} , {player_one: {'sides':3,'num':10} , player_two : {'sides':6,'num':1}} , {player_one: {'sides':4,'num':1} , player_two : {'sides':20,'num':1}} ]

def simulate_general_Euler_205(case:dict , num_of_experiments:int = 1_000_000): # Do Not Change this function ''' This function simulates the probability of Dice Game, i.e. Euler 205. ''' def general_dice(sides:int=6): # Do Not Change this function ''' This function implements a general dice function ''' return lambda : rand.randint(1,sides) if sides > 1 else None def get_dice_collections(): # Do Not Change this function ''' Each player has a collection of dice. This function returns the collections in a dictionary. ''' dice_peter = general_dice(case[player_one]['sides']) dice_colin = general_dice(case[player_two]['sides']) return {player_one: [dice_peter]*case[player_one]['num'] , player_two:[dice_colin]*case[player_two]['num']}

def calculate_probability(): ''' This function calculates the probability of winning for one given case. ''' winning_probability = {player_one:0,player_two:0,'Draw':0} # todo 2: Complete the implemetation of this function # start here ..............

# finish here .............. return winning_probability # the result of the simulation will be returned here return calculate_probability() # do not change this

def find_num_dice_equal_prob(case , max_num_dice=30): # probability of the given case will be calculated here probs = simulate_general_Euler_205(case) message_init_case = 'Initial result for case:'+str(case)+' With Probabilities:'+str(probs) fair_case = case fair_probs = probs

# todo 3: complete the function to find a case for a fair game # hint: iterate in a loop for max_num_dice and find number of dice for both players such that they play a fair game # in each iteration call simulate_general_Euler_205 and check probabilities, increase number of dice for minimum probability, # then call the function again with a new case. # At the end fair_case will contain information of case with updated number of dice and fair_probs will contain probabilities of fair case.

# start here .............. #

# finish here .............. #here fair_case and fair_probs must be ready with the results of the search message_fair_case = 'Final result for fair game in case:'+str(fair_case)+' With Probabilities:'+str(fair_probs) return message_init_case+os.linesep+message_fair_case

def main(): # Do Not change this function ''' This function checks student number, iterates over test cases and stores the final results in a log file. ''' if len(std_number)!=7 or len(set(std_number)-set(string.digits)): print('ERROR: student number is not valid') return

log='' log_ext = '.txt' log_name = 'dice_game_' out_file_name = log_name + std_number + log_ext # iterate over the test cases for case_num in range(0,len(test_cases)): log = log + os.linesep*2 + 'CASE '+str(case_num)+os.linesep*2 + find_num_dice_equal_prob(test_cases[case_num]) # print the result print(log) # store the result in a log file with open(out_file_name,'w') as log_file: log_file.write(log) log_file.close() main() # Do Not change this function

# This code template is created for Challenge 2: Dice Game. #

The template must be complete import random as rand import string import

Project Euler: Problem 205

os # global variables # todo 1: std_number must have the correcthelp me plsss and show output as well and the code so i can copy past easily do in Python

Note The base of this challenge is Project Euler: Problem 205. Objectives: - Evaluate your skill in applying concepts of: simulation of probabilistic experiments, dictionaries, functions and higher-order functions. Problem Introduction: Peter has nine four-sided (pyramidal) dice, each with faces numbered 1,2,3,4. Colin has six six-sided (cubic) dice, each with faces numbered 1,2,3,4,5,6. Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal. Step 1: Implement a program that simulates the problem and prints the probability that Pyramidal Peter beats Cubic Colin? Step 2: Peter and Colin would like to play a fair game. How many dice each must have in order to have equal (with precision of 2 decimal) probability of winning? Extend your program such that can find a solution for Peter and Colin. For example Peter with 14 four-sided dice and Colin with 10 six-sided dice can have a fair game with probability of winning of 0.47 for each. You should note that number of dice ( 14 and 10) are not the only solutions. Depending on how your search strategy is, you may find 7 for Peter and 5 for Colin. Both are correct. Another case would be: - Peter with 4 four-sided dice and Colin with 2 nine-sided dice can have a fair game with probability of winning of 0.45 for each. - Peter with 6 four-sided dice and Colin with 3 nine-sided dice can have a fair game with probability of winning of 0.46 for each. - Peter with 2 four-sided dice and Colin with 1 nine-sided dice can have a fair game with probability of winning of 0.44 for each. Step 3: Generalize your program such that: 1. It can start with a any type and any number of dice. 2. It can find a solution for a fair game. Step 4: Refactor your code with applications of higher order functions. Note: Details of the structure of the code and some test cases will be published later. Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4 . Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6 . Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal. What is the probability that Pyramidal Pete beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!