PSTAT 160A PYTHON HOMEWORK

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
PSTAT 160A作业代写、代做PYTHON编程作业、PYTHON课程设计作业代写
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!

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:[email protected]

微信:codinghelp

原文地址:https://www.cnblogs.com/xifua/p/10886795.html

时间: 2024-09-30 06:01:16

PSTAT 160A PYTHON HOMEWORK的相关文章

Python之ftp服务器

今天把做的ftp服务器过程总结一下,先看看要求 一.需求 1. 用户加密认证 2. 允许同时多用户登录 3. 每个用户有自己的家目录 ,且只能访问自己的家目录 4. 对用户进行磁盘配额,每个用户的可用空间不同 5. 允许用户在ftp server上随意切换目录cd 6. 允许用户查看当前目录下文件ls 7. 允许上传put和下载get文件,保证文件一致性 8. 文件传输过程中显示进度条 附加实现的功能: 1.实现服务器端文件删除,创建.删除目录 2.查看当前账户状态 3.实现断点续传 二.实现过

python day 1 homework 2

多级菜单 1 三级菜单 2 可依次选择进入各子菜单 3 所需新知识点,列表,字典 province_info = {"1":{"name":"黑龙江", "city":{"1":{"name":"哈尔滨","area":"道里区.南岗区.道外区.平房区.松北区.香坊区.呼兰区.阿城区.双城区"}, "2":{

Python之路【第七篇续】:I/O多路复用

回顾原生Socket 一.Socket起源: socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用[打开][读写][关闭]模式来操作. socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO.打开.关闭) “他是所有WEB服务器的祖宗” pupepet.ansible.他们也可以通过输入命令然后返回结果这个也是基于Socket来实现的. 二.socket和file的区别:    file模块是针

Python实战作业-day5

作业需求: 用户登录注册页面 首页.注册页面.登录页面:后端存储数据是mysql数据库 作业代码: 1 [email protected]~/reboot15/day5/homework$cat app.py 2 #!/usr/bin/env python 3 #coding:utf-8 4 ''' 5 作业需求: 6 基于mysql数据库实现用户登录模块,并实现针对于用户信息的增删改查 7 ''' 8 9 10 from flask import Flask,render_template,r

[学习笔记] Python标准库简明教程 [转]

1 操作系统接口 os 模块提供了一系列与系统交互的模块: >>> os.getcwd() # Return the current working directory '/home/minix/Documents/Note/Programming/python/lib1' >>> os.chdir('~/python') # Change current working directory Traceback (most recent call last): File

python核心编程一书笔记之第一篇

#!/usr/bin/env python# -*- coding:utf-8 -*- #env 是一个命令用来寻找系统中的python解释器.第二条解释使用utf-8编码 在类unix系统中允许python为了防止出现找不到命令经常会加上一段申明,在python3以下的版本需要加上编码申明来实行兼容: 在python中的第一个案例: 1 print 'Hello World!' 2 #你好这个世界 嘿嘿! print 这个命令用于输出,常用来查看变量里面的内容,或者输出一些内容显示给用户. 变

Python面向对象-day07

写在前面 上课第七天,打卡: 时间的高效利用: 前言: 今天egon老师补充了下 is 和 == 的区别,整理如下:Python中变量的属性以及判断方法 一.面向过程和面向对象 - 1.面向过程 核心是过程,过程就是解决问题的步骤:流水线.机械式: 优点:复杂的问题简单化,分成多个功能单元: 缺点:可扩展性差 应用:Linux内核.httpd.git - 2.面向对象 核心是对象: 要理解对象,应该把自己当做上帝,在上帝眼里一切存在的事物都是对象,不存在的也可以创建出来: 对象是 特征(变量)和

Python的平凡之路(13)

一.Python的paramiko模块介绍 Python 的paramiko模块,该模块和SSH用于连接远程服务器并执行相关操作 SSH client 用于连接远程服务器并执行基本命令 基于用户名和密码的SSH连接代码如下: 1 import paramiko 2 3 ssh = paramiko.SSHClient() #创建SSH对象. 4 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在know_hosts文

Python学习笔记之基本语法

函数导入的三种方式 from math import sqrt   #import the sqrt function only     e.g. sqrt(25) from math import *     #import all functions from math module     e.g. sqrt(25) import math         #import math module,and use the function via math.*       e.g. math