-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment2.py
73 lines (57 loc) · 1.37 KB
/
Assignment2.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
# Assignment 2 Python 3
# Ethan Otto 1/31/18
#10.3
def cum_sum(x):
cum_sum=0
list_cum = []
length = len(x)
i = 0
for e in x:
assert type(e) == int or type(e) == float
cum_sum +=e
list_cum.append(cum_sum)
i += 1
if i == length:
return list_cum
t= cum_sum([1,2,3,4,5,6,7,8,9,10])
t
# 2D Class object Python 3
class Point(object):
"""A 2D Point object"""
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def __str__(self):
string = "{x: %d, y: %d }" % (self.x,self.y)
return string
def __add__(self, other):
assert isinstance(other,Point)
p = Point()
p.x = self.x + other.x
p.y = self.y + other.y
return p
def __sub__(self, other):
assert isinstance(other,Point)
p = Point()
p.x = self.x- other.x
p.y = self.y - other.y
return p
def __mul__(self, num):
assert isinstance(num, (float,int))
p = Point()
p.x = self.x*num
p.y = self.y*num
return p
def __rmul__(self,num):
return self*num
def dot(self,other):
assert isinstance(other,Point)
dot = self.x*other.x + self.y*other.y
return dot
p1 = Point(4,10)
p2 = Point(1,8)
print(p1+p2)
print(p1-p2)
print(p1*4)
print(4*p1)
p1.dot(p2)