Question

In: Computer Science

Write a python script to calculate the average length of the game shortest game , longest...

Write a python script to calculate the average length of the game shortest game , longest game and the overall average length of a snake and ladder game

The game length is the number of throws of the die. We are asked to write a python script which calculate that together with the average

Solutions

Expert Solution

I am not clear with the question but I can help you calculate the average as mentioned in the problem:

The is as follows:

Because as a parent one gets roped into these board (boring?) games
every so often, and I wanted to calculate the average duration of a
snakes and ladders game. Turns out it's about 36 moves (though
admittedly that's for a single-player game). :-)
> python snakes_and_ladders.py
Played 10000 rounds, averaged 36.0559 moves, max 324 moves, took 0.508s
"""
from __future__ import division
import random
import time
try:
range = xrange # Make range a generator on Python 2.7
except NameError:
pass
# Defines the snakes and ladders on the board. A low:high item
# represents a ladder going from low to high, whereas a high:low
# item represents a snake going from high to low.
SNAKES_LADDERS = {
1: 38, 4: 14, 9: 31,
16: 6,
21: 42, 28: 84,
36: 44,
48: 26, 49: 11,
51: 67, 56: 53,
62: 19, 64: 60,
71: 91, 80: 100,
87: 24,
93: 73, 95: 75, 98: 78,
}
MAX_SQUARE = 100
MAX_MOVES = 100000
def play_one():
"""Play one game and return the number of moves it took."""
square = 0
for num_moves in range(1, MAX_MOVES + 1):
dice_roll = random.randrange(1, 7)
square += dice_roll
square = SNAKES_LADDERS.get(square, square)
if square >= MAX_SQUARE:
return num_moves
return MAX_MOVES
def play_all(num_rounds):
"""Play num_rounds games and return tuple of (total, max, time)."""
start_time = time.time()
total_moves = 0
max_moves = 0
for _ in range(num_rounds):
num_moves = play_one()
total_moves += num_moves
max_moves = max(max_moves, num_moves)
elapsed_time = time.time() - start_time
return (total_moves, max_moves, elapsed_time)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--num-rounds', type=int, default=10000,
help='number of rounds (games) to play')
args = parser.parse_args()
total_moves, max_moves, elapsed_time = play_all(args.num_rounds)
print('Played {} rounds, averaged {} moves, max {} moves, took {:.3f}s'.format(
args.num_rounds,
total_moves / args.num_rounds,
max_moves,
elapsed_time,
))

Related Solutions

Write a python script to calculate the average length of the game, Shortest game, longest game...
Write a python script to calculate the average length of the game, Shortest game, longest game and overall length of the game we need a python script that calculates the average length of a snake and ladder game. ie the number of moves it takes a single player to win the game. Then you run the game several times and will compare the results to come up with the required data(length of the game and number of moves )
I am struggling to write a python script that calculates the average length ,max length shortest...
I am struggling to write a python script that calculates the average length ,max length shortest length and overall average length of of a snake and ladder game. I use python 3 The length is the number the dice is rolled until the max is reached We need a python script that calculates the number of moves it will take for a single player to reach 100. We have to simulate a snake and ladder game using python language. So...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
write a python script to calculate 401k with compounding interest
write a python script to calculate 401k with compounding interest
Write a python code to generate a random undirected weighted graph and calculate the shortest distance...
Write a python code to generate a random undirected weighted graph and calculate the shortest distance from node i to node j (shortest path algorithm) where i is not equal to j. Store the results in text file. Thank you!
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
write a python script to calculate the return of a stock with 10years of historical data/returns
write a python script to calculate the return of a stock with 10years of historical data/returns
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT