Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Lab Exercises Week 8.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Week8_Lab
# Week8_Lab Solutions
3 changes: 3 additions & 0 deletions aNewFolder/fileinfolder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#newfloder with a file in it

There is some words in the file.
34 changes: 34 additions & 0 deletions ccuboid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 12:31:56 2016

@author: Mebius
"""
import random
from cuboid import Cuboid

class CCuboid(Cuboid):
def __init__(self, x,y,z, color ):
Cuboid.__init__(self, x , y , z )
self.color = color

def set_color(self, color):
self.color = color

def get_color(self):
return self.color

def __str__(self):
a_str = super().__str__() + \
', Color: ' + str(self.get_color())
return a_str


if __name__ == "__main__":

x=random.randint(1,20)
y=random.randint(1,20)
z=random.randint(1,20)
color= input("Input your color: ")
a = CCuboid(x,y,z,color)
print("The color of the cuboid is", a.get_color())
34 changes: 17 additions & 17 deletions cuboid_student.py → cuboid.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 12:29:33 2016
Created on Thu May 19 13:07:12 2016

@author: Mebius
"""
Expand All @@ -12,13 +12,12 @@ def __init__(self,x,y,z):
self.x=x
self.y=y
self.z=z

#implement this!

def get_area(self):
pass
#implement this!
return 2*self.x*self.y +2*self.x*self.z +2*self.z*self.y

def get_volume(self):
pass
return self.x*self.y*self.z

def __str__(self):
c_str = 'Length: ' + str(self.x) + \
Expand All @@ -30,27 +29,28 @@ def __str__(self):

class CCuboid(Cuboid):
def __init__(self, x,y,z, color ):
Cuboid.__init__(self, x , y , z )
pass
Cuboid.__init__(self, x , y , z )
#super().__init__(x, y, z)
self.color = color

def set_color(self, color):
pass
self.color = color

def get_color(self):
pass
return self.color

def __str__(self):
a_str = super().__str__() + ', Color: ' + str(self.get_color())
a_str = super().__str__() + \
', Color: ' + str(self.get_color())
return a_str

if __name__ == "__main__":

x=1
y=2
z=3

c = Cuboid(x,y,z)
print(c)
x=random.randint(1,20)
y=random.randint(1,20)
z=random.randint(1,20)
#c = Cuboid(x,y,z)
#print(c)

color= input("Input your color: ")

Expand Down
82 changes: 43 additions & 39 deletions dice_student.py → dice.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 19 15:23:36 2016

@author: zhengzhang
"""
import random
import dice_helper

class Dice:
def __init__(self, sides=2):
self.n_sides = sides
self.bounds = [x/sides for x in range(0, sides)]
self.bounds.append(1.0)
self.point = None
self.lands = 0

def set_bounds(self, r):
assert len(r) == len(self.bounds)
self.bounds = r

def get_bounds(self):
return self.bounds

def roll(self):
# replace the following line with you code
# it should set self.point as a random var in [0, 1]
# return which side the dice lands on
return dice_helper.roll(self)

if __name__ == "__main__":
d = Dice()
''' make a biased dice '''
d.set_bounds([0.0, 0.3, 1.0])
ones = 0
num_rolls = 1000
for i in range(num_rolls):
ones += d.roll()
print("the dice is:", ones/float(num_rolls))
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 13:42:25 2016

@author: Mebius
"""

import random

class Dice:
def __init__(self, sides=2):
self.n_sides = sides
self.bounds = [x/sides for x in range(0, sides)]
self.bounds.append(1.0)
self.point = None
self.lands = 0

def set_bounds(self, r):
assert len(r) == len(self.bounds)
self.bounds = r

def get_bounds(self):
return self.bounds

def roll(self):
self.point = random.uniform(0, 1)
for i in range(self.n_sides):
if self.point > self.bounds[i] \
and self.point <= self.bounds[i+1]:
break
self.lands = i
return self.lands


if __name__ == "__main__":
d = Dice()
''' make a biased dice '''
d.set_bounds([0.0, 0.3, 1.0])
ones = 0
num_rolls = 10
for i in range(num_rolls):
ones += d.roll()
print("the dice is:", ones/float(num_rolls))

156 changes: 79 additions & 77 deletions random_walk_student.py → random_walk.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,78 +1,80 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 19 16:14:38 2016

@author: zhengzhang
"""
import dice_student
import math
import matplotlib.pyplot as plt

import random_walk_helper

class Walker:
def __init__(self, directions=4, num_steps=10):
self.dice = dice_student.Dice(directions)
self.num_steps = num_steps
self.x_pos = 0
self.y_pos = 0

def set_bounds(self, r):
self.dice.set_bounds(r)

def run(self):
# replace the following line with you code
# roll the dice, according to the outcome:
# - go left one step (x -= 1)
# - go right one step (x += 1)
# - go north one step (y += 1)
# - go south one step (y -= 1)
random_walk_helper.run(self)

def get_position(self):
return self.x_pos, self.y_pos

def get_dist(self):

def comp_dist(a, b):
assert len(a) == len(b)
d = 0
for i in range(len(a)):
d += (a[i] - b[i]) ** 2
return math.sqrt(d)

return comp_dist([0, 0], [self.x_pos, self.y_pos])

if __name__ == "__main__":
one_walker = Walker()
one_walker.run()
print("one walk", one_walker.get_dist())

num_steps = 100
total_walks = 2000
result = []
x_pos_list = []
y_pos_list = []

weights = [0.0, 0.25, 0.5, 0.75, 1.0]

for i in range(total_walks):
one_walker = Walker(num_steps=num_steps)
one_walker.set_bounds(weights)
one_walker.run()
x, y = one_walker.get_position()
x_pos_list.append(x)
y_pos_list.append(y)
result.append(one_walker.get_dist())

print("mean distance:", sum(result)/total_walks)
plt.subplot(2, 1, 1)
plt.hist(result)
plt.title('position distribution')

plt.subplot(2, 1, 2)
plt.title('2D distribution')
plt.scatter(x_pos_list, y_pos_list)

plt.show()
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 27 13:41:17 2016

@author: Mebius
"""

import dice
import math
import matplotlib.pyplot as plt

class Walker:
def __init__(self, directions=4, num_steps=10):
self.dice = dice.Dice(directions)
self.num_steps = num_steps
self.x_pos = 0
self.y_pos = 0

def set_bounds(self, r):
self.dice.set_bounds(r)

def run(self):
for i in range(self.num_steps):
direction = self.dice.roll()
if direction == 0:
self.x_pos += 1
elif direction == 1:
self.x_pos -= 1
elif direction == 2:
self.y_pos += 1
elif direction == 3:
self.y_pos -= 1

def get_position(self):
return self.x_pos, self.y_pos

def get_dist(self):

def comp_dist(a, b):
assert len(a) == len(b)
d = 0
for i in range(len(a)):
d += (a[i] - b[i]) ** 2
return math.sqrt(d)

return comp_dist([0, 0], [self.x_pos, self.y_pos])

if __name__ == "__main__":
one_walker = Walker()
one_walker.run()
print("one walk", one_walker.get_dist())

num_steps = 100
total_walks = 2000
result = []
x_pos_list = []
y_pos_list = []

weights = [0.0, 0.25, 0.5, 0.75, 1.0]

for i in range(total_walks):
one_walker = Walker(num_steps=num_steps)
one_walker.set_bounds(weights)
one_walker.run()
x, y = one_walker.get_position()
x_pos_list.append(x)
y_pos_list.append(y)
result.append(one_walker.get_dist())

print("mean distance:", sum(result)/total_walks)
plt.subplot(2, 1, 1)
plt.hist(result)
plt.title('position distribution')

plt.subplot(2, 1, 2)
plt.title('2D distribution')
plt.scatter(x_pos_list, y_pos_list)

plt.show()