联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-05-16 06:02

PYTHON HOMEWORK 5 – DUE 5/15 AT 5:00 PM

PSTAT 160A – S19

Professor Hohn

Instructions: Please note that you must work by yourself ! You will submit two files on GauchoSpace:

(1) clear and concise explanations, graphics (if any), and results in PDF format (worth

40 points in total) and (2) your Python code in .py format (10 points). Note that if the grader

finds identical copies or very similar files, the grader cannot and will not grade them.

For Question #1, be sure to show all of your work! You may either (a) write at least a couple

sentences explaining your reasoning or (b) annotate your math work with brief explanations. Please

label any random variables or events that your use.

Background: The children’s game Chutes and Ladders is based on an ancient Indian game called

Snakes and Ladders (see https://en.wikipedia.org/wiki/Snakes_and_Ladders). The game is

played on a 100-square board. Each player has a token and takes turns rolling a six-sided die and

moving their token by the corresponding number of squares. If a player lands on a ladder, they

immediately move up the ladder to a higher-numbered square. If they move to a chute, or snake,

they drop down to a lower-numbered square. The finishing square 100 must be reached by an exact

roll of the die (or by landing on square 80 whose ladder climbs to the finish). The first player to

land on square 100 wins.

The game is a Markov chain since the player’s position only depends on their previous position and

the roll of the die. The chain has 101 states as the game starts will all players off the board (state

0).

1. (10 points) (Without Python) The board for a modified Snakes and Ladders game is shown

in Figure 1. The game is played with a tetrahedron (4-sided) die. Like the original, players

start off the board, and the finishing square 9 must be reached by an exact roll of the die. If

the die roll is too large, the player’s token goes toward the final square and reverses back again.

(For example, if a player requiring a 3 to win rolls a 5, the token moves forward three spaces,

then back two spaces.)

Figure 1: Modified Snakes and Ladders game board

(a) Find the expected length of the modified Snakes and Ladders game. That is, what is the

average number of plays/moves needed to reach the finish.

(b) Assume that your friend Chidi is on square 6. Find the probability that Chidi will find

himself on square 3 before finishing the game.

2. (30 points) (With Python) In this Python exercise, you will be estimating the average number

of plays/moves until the modified Snakes and Ladders game is finished and the probability that

a person starting at square 6 will find themselves on square 3 before finishing the game. Be

sure to annotate your code with short explanations of what you are doing (worth 10 points).

Suppose (as above) that we are playing Snakes and Ladders on a modified game board.

(a) Simulate playing 10,000 games. Compute the average number of moves until the game is

finished. Use the print function to print your answer. Be sure to label your results. For

example,

print(’The average number of moves before a game is finished is %s.’

% expectedNumberOfMoves)

(b) Simulate playing 10,000 games, but this time, start each game from square 6. Compute

the probability that a person starting at square 6 will find themselves on square 3 before

finishing the game. Use the print function to print your answer.

(c) Take a screenshot showing your code and your results together (e.g. side by side).

Python Code Hints

numpy.linalg.solve(A,b)

will solve the matrix equation Ax = b.

numpy.identity(n)

will give you the n × n identity matrix.

numpy.matrix

will return a matrix from an array-like object or from a string of data. For example,

a = numpy.matrix([[1, 2], [3, 4]])

will give you the matrix . It has certain special operators, such as * (matrix

multiplication) and ** (matrix power).

Change a matrix to a list via

nameOfMyMatrix.tolist()[0]

One way to make a bar graph is to use the library matplotlib.

matplotlib.pyplot.bar( x, height = y, align=’center’, alpha=0.5, color=’g’)

produces a bar graph with x-axis described by x and y axis described by y. Both x and y

are lists here.

matplotlib.pyplot.ylabel(’Label Me’)

creates a label for the y axis called Label Me.

Page 2

matplotlib.pyplot.title(’Title Me’)

creates a title for the graph called Title Me.

matplotlib.pyplot.xticks(x, listOfNames)

will label the x axis tick marks with a list of names (e.g. Port numbers).

matplotlib.pyplot.show()

shows the graph/plot.

numpy.random.choice( alist, p = alist_prob )

will pick one element from the set alist using the probability distribution alist prob.

When Python indexes a list, the index of the list starts at 0. That is, to access the first

entry of a list like a=[7, 8, 9], we need to write a[0].

alist = [1, 4, 7]

alist.append( 3 )

will add 3 to the end of your list. So, alist = [1, 4, 7, 3].

set()

creates a set – an unordered collections of unique elements.

aSet = set( [’Stark’, ’Lannister’, ’Greyjoy’] )

aSet.add( ’Baratheon’ )

will add Baratheon to the set aSet.

alist = [1, 4, 7, 1, 4, 4]

alist.count( 4 )

returns 3, the number of times 4 occurs in the list alist.

len (alist)

return the length (the number of items) of alist. alist can be a sequence (such as a list)

or a collection (like a set).

You may need to use a for loop or while statement in you code. See

http://www.openbookproject.net/books/bpp4awd/ch04.html for examples.

If you are using Python 2, you’ll need to import division from Python 3 so that it acts

like Python 3 when using the / symbol in your calculations. If you are using Python 2,

write at the top of your py file,

from __future__ import division

You’re importing the future!

Page 3


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp