/
a08q3.py
178 lines (133 loc) · 5.34 KB
/
a08q3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import check
####################################
## Frances Ferrabee 20712523
## CS 116 Winter 2018
## Assignment 08 Problem 03
####################################
#q3
class Vector:
'''Fields: x(Int), y(Int)'''
def __init__(self,xVal,yVal):
self.x = xVal
self.y = yVal
def __eq__(self,other):
if isinstance(other,Vector):
return self.x == other.x and self.y == other.y
else:
return False
def __repr__(self):
return "Vector({0},{1})".format(self.x,self.y)
#q3b
## __add__(self, others): returns the Vector obtained by adding 2 Vectors together
## __add__: Vector Vector -> Vector
##Examples:
## Vector(2,3)+ Vector(4,-1) -> Vector(6,2)
## Vector(0,-5)+ Vector(-2,1) -> Vector(-2,-4)
def __add__(self,other):
return Vector(self.x+ other.x, self.y+ other.y)
## __mul__(self, other): returns the dot product of 2 Vectors
## __mul__: Vector Vector -> Int
## Examples:
## Vector(2,3) * Vector (4, -1)) -> 5
## Vector(0, -5)* Vector(-2, 1) -> -5
def __mul__(self,other):
return self.x* other.x+ self.y* other.y
## length(): returns the length of the vector
## length: Vector -> Float
## Examples:
## Vector(1,1).length() -> about 1.41
## Vector(2,1).length() -> about 2.23
def length(self):
return (self.x**2+ self.y**2)**.5
## scale(self,k): returns nothing
## Effects: It will mutate vec so that both the x and y coordinates are
##* multiplied by k
## scale: Vector Int -> None
## Examples:
## Vector(2,2).scale(2) mutates Vector(2,2) so it is Vector(4,4)
## Vector(-1,0).scale(1) mutates Vector(-1,0) so it is Vector(-1,0)
def scale(self,k):
self.x= self.x* k
self.y= self.y*k
## Tests for B
check.expect("Test1", Vector(1,1) + Vector(2,1), Vector(3,2))
check.expect("Test2", Vector(0,0) + Vector(2,1), Vector(2,1))
check.expect("Test3", Vector(1,2) + Vector(0,0), Vector(1,2))
check.expect("Test4", Vector(0,0) + Vector(0,0), Vector(0,0))
check.expect("Test5", Vector(-3,-3) + Vector(2,1), Vector(-1,-2))
check.expect("Test1", Vector(1,1) * Vector(2,1), 3)
check.expect("Test2", Vector(0,0) * Vector(2,1), 0)
check.expect("Test3", Vector(1,2) * Vector(0,0), 0)
check.expect("Test4", Vector(0,0) * Vector(0,0), 0)
check.expect("Test5", Vector(-3,-3) * Vector(2,1), -9)
check.within("Test1", Vector(1,2).length(), 2.23, 0.1)
check.within("Test2", Vector(-2,4).length(), 4.47, 0.1)
check.within("Test3", Vector(-3,-1).length(), 3.16, 0.1)
check.within("Test4", Vector(2,2).length(), 2.83, 0.1)
c= Vector(1,2)
check.expect("Test1", c.scale(2), None)
check.expect("Test1", c, Vector(2,4))
d= Vector(3,4)
check.expect("Test2", d.scale(0), None)
check.expect("Test2", d, Vector(0,0))
d= Vector(1,0)
check.expect("Test3", d.scale(2), None)
check.expect("Test3", d, Vector(2,0))
#q3a
## add_vectors(v1, v2): returns the Vector obtained by adding 2 Vectors together
## add_vectors: Vector Vector -> Vector
##Examples:
## add_vectors(Vector(2,3), Vector(4,-1)) -> Vector(6,2)
## add_vectors(Vector(0,-5), Vector(-2,1)) -> Vector(-2,-4)
def add_vectors(v1,v2):
return Vector(v1.x+ v2.x, v1.y+ v2.y)
check.expect("Test1", add_vectors(Vector(1,1), Vector(2,1)), Vector(3,2))
check.expect("Test2", add_vectors(Vector(0,0), Vector(2,1)), Vector(2,1))
check.expect("Test3", add_vectors(Vector(1,2), Vector(0,0)), Vector(1,2))
check.expect("Test4", add_vectors(Vector(0,0), Vector(0,0)), Vector(0,0))
check.expect("Test5", add_vectors(Vector(-3,-3), Vector(2,1)), Vector(-1,-2))
## dot_product(v1, v2): returns the dot product of 2 Vectors
## dot_product: Vector Vector -> Int
## Examples:
## dot_product(Vector(2,3), Vector (4, -1)) -> 5
## dot_product(Vector(0, -5), Vector(-2, 1)) -> -5
def dot_product(v1,v2):
return v1.x* v2.x+ v1.y* v2.y
check.expect("Test1", dot_product(Vector(1,1), Vector(2,1)), 3)
check.expect("Test2", dot_product(Vector(0,0), Vector(2,1)), 0)
check.expect("Test3", dot_product(Vector(1,2), Vector(0,0)), 0)
check.expect("Test4", dot_product(Vector(0,0), Vector(0,0)), 0)
check.expect("Test5", dot_product(Vector(-3,-3), Vector(2,1)), -9)
## length(vec): returns the length of the vector
## length: Vector -> Float
## Examples:
## length(Vector(1,1)) -> about 1.41
## length(Vector(2,1)) -> about 2.23
def length(vec):
return (vec.x**2+ vec.y**2)**.5
check.within("Test1", length(Vector(1,2)), 2.23, 0.1)
check.within("Test2", length(Vector(-2,4)), 4.47, 0.1)
check.within("Test3", length(Vector(-3,-1)), 3.16, 0.1)
check.within("Test4", length(Vector(2,2)), 2.83, 0.1)
## scale(vec,k): returns nothing
## Effects: It will mutate vec so that both the x and y coordinates are
##* multiplied by k
## scale: Vector Int -> None
## Examples:
## scale(Vector(2,2), 2) mutates Vector(2,2) so it is Vector(4,4)
## scale(Vector(-1,0), 1) mutates Vector(-1,0) so it is Vector(-1,0)
def scale(vec,k):
vec.x= vec.x* k
vec.y= vec.y * k
c= Vector(1,2)
check.expect("Test1", scale(c, 2), None)
check.expect("Test1", c.x, 2 )
check.expect("Test1", c.y, 4)
d= Vector(3,4)
check.expect("Test2", scale(d, 0), None)
check.expect("Test2", d.x, 0)
check.expect("Test2", d.y, 0)
d= Vector(1,0)
check.expect("Test3", scale(d, 2), None)
check.expect("Test3", d.x, 2 )
check.expect("Test3", d.y, 0)