Only do Q2 please!!! please Type the code in python!!!thanks
Only do Q2 please!!! please Type the code in python!!!thanks
Doing 1 just to establish the theoretical value
1. Let T be the time in Dungeon.
Let D={A,B,C} be the door taken with probability P(D=A) = P(D=B)=P(D=C)=1/3
The expectation of T can be written as
If D=A, then Eddard wanders around for 3 days and returns back to the start and hence would take on an average E(T) more before he gets out
E(T|D=A) = 3+E(T)
If D=B, then Eddard wanders around for 4 days and returns back to the start and hence would take on an average E(T) more before he gets out
E(T|D=B) = 4+E(T)
If D=C, then Eddard would find the exit in 2 days
E(T|D=C) = 2
Hence
by solving the equation we get
Eddard would take on an average 9 days to get out.
2) Now the Python code with comments is below
Get the following output
The result is close enough to the theoretical value calculated in 1)
The code in text format (the tabs may be lost)
import numpy as np
#define a function to randomly choose a door
def choosedoor():
#get a uniform random number in the interval (0,1)
u=np.random.random()
# choose the door A if 0<=u<1/3, B if 1/3<=u<2/3, C if
2/3<=u<1
if u<1/3:
return('A')
elif u<2/3:
return('B')
else:
return('C')
# function to get the total time spent in the dungeon
def dungeon(door,days):
# if door C get out in 2 days
if door=='C':
days=days+2
#if door A take 3 days and go back to start by recursively call
back the function
elif door=='A':
days=days+3
door=choosedoor()
days=dungeon(door,days)
#if door B take 4 days and go back to start by recursively call
back the function
else:
days=days+4
door=choosedoor()
days=dungeon(door,days)
return(days)
# set the seed for the random number generator
np.random.seed(123)
# set the number of simulations
N=10000
#initialize a variable to hold the time spent
TimeInDungeon=[]
# simulate the run for N times
for i in range(N):
# choose a door for the first time
door=choosedoor()
#call the function dungeon and store the time spent
TimeInDungeon.append(dungeon(door,0))
#find the average time spent
avergaeTimeInDungeon=np.mean(TimeInDungeon)
print("After 10,000 simulations, the average time it takes Eddard
to escape is %s days."%avergaeTimeInDungeon)
Only do Q2 please!!! please Type the code in python!!!thanks Only do Q2 please!!! please Type...
Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...
Python 3. Help please. Here is my current code. What can I do to fix it? I list=[1,2,2,3,4,5] d = {} for item in list: if item in d: return True d[item] = True return False Exercise 11.4. If you did Exercise 10.7, you already have a function named has_duplicates that takes a list as a parameter and returns True if there is any object that appears more than once in the list. Use a dictionary to write a faster,...
Please answer this question using python programming only and provide a screen short for this code. Homework 3 - Multiples not less than a number Question 1 (out of 3) This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand. Use this template to write a Python 3 program that calculates the nearest multiple of a number that...
Please help me write these in R script / Code 1, Suppose you're on a game show, and you're given the choice of three doors. Behind one door is a car; behind the others, goats. You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat. He then says to you, "Do you want to pick door #2?" What is the probability of winning the car if...
Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...
Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...
how to make my code of python work probely my q is how to write a code to find the average actual water level, average error by using python and my program should be flexible where user can select the column no. If you can see in the main program, it asked which column to know the maximum or average, the column number is passed to the function. The function should look which column to do. #THIS PROGRAMMING IS ABLE...
please use python thanks will rate!! x + Run C Code Validate Implement the function step_random_walk_20(x_coords, Y_coords) below, which should take two arrays of equal length containing the x-andy- coordinates for some number of particles. We'll use a very simple random walk algorithm • For each particle, choose a random angle between 0 and 2 • The particle moves by 1 unit of distance in the direction given by d.o. It is displaced by (Ax, Ay) (cos, sino). We'll do...
please help me write the python code for this problem Problem 2: Restaurant Tipping IAE IOL (Fall 2015) HR . Problem 2 After many years, you have developed a system for determining exactly how much to tip when eating out at a restaurant: de tip mount bill, good service ADD YOUR COOE MERE return -1. CHANGE OR REMOVE THIS LINE • If the final bill is $30.00 or less, you will leave a tip of exactly $5.00 (regardless of the...
this is a python question please help me with this code thank you and appreciate it 1. Write a Boolean function between that takes two My Time objects, t1 and t2, as arguments, and returns True if the invoking object falls between the two times. Assume t1 <= t2, and make the test closed at the lower bound and open at the upper bound, i.e. return True if t1 <= obj <t2. 2. Turn the above function into a method...