diff --git a/Lab Exercises Week 8.pdf b/Lab Exercises Week 8.pdf index ca7ac80..1577acf 100644 Binary files a/Lab Exercises Week 8.pdf and b/Lab Exercises Week 8.pdf differ diff --git a/README.md b/README.md index 444754a..4da1539 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# Week8_Lab \ No newline at end of file + +# Week8_Lab +### Hi there, I am from "newbranch". + +#### In the meanwhile, you also did some modifications in the newbranch + +#### Master branch: some modifications have been done + +##### check the conflicts then, delete the >>> and <<<, you can resolve the problem diff --git a/aNewFolder/fileinfolder b/aNewFolder/fileinfolder new file mode 100644 index 0000000..e209410 --- /dev/null +++ b/aNewFolder/fileinfolder @@ -0,0 +1,3 @@ +#newfloder with a file in it + +There is some words in the file. diff --git a/ccuboid.py b/ccuboid.py new file mode 100755 index 0000000..da2fcff --- /dev/null +++ b/ccuboid.py @@ -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()) diff --git a/cuboid_student.py b/cuboid.py old mode 100644 new mode 100755 similarity index 62% rename from cuboid_student.py rename to cuboid.py index 324b287..afaddf5 --- a/cuboid_student.py +++ b/cuboid.py @@ -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 """ @@ -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) + \ @@ -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: ") diff --git a/dice_student.py b/dice.py old mode 100644 new mode 100755 similarity index 65% rename from dice_student.py rename to dice.py index a249f60..1074e97 --- a/dice_student.py +++ b/dice.py @@ -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)) \ No newline at end of file diff --git a/random_walk_student.py b/random_walk.py old mode 100644 new mode 100755 similarity index 74% rename from random_walk_student.py rename to random_walk.py index 43e7bc4..940e92f --- a/random_walk_student.py +++ b/random_walk.py @@ -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() \ No newline at end of file