@@ -46,22 +46,28 @@ def __init__(self, value):
46
46
def __add__ (self , other ):
47
47
if isinstance (other , Int ):
48
48
return Int (self .value + other .value )
49
+ elif isinstance (other , Float ):
50
+ return Float (self .value + other .value )
49
51
raise TypeError ()
50
52
51
53
def __sub__ (self , other ):
52
54
if isinstance (other , Int ):
53
55
return Int (self .value - other .value )
56
+ elif isinstance (other , Float ):
57
+ return Float (self .value - other .value )
54
58
raise TypeError ()
55
59
56
60
def __mul__ (self , other ):
57
61
if isinstance (other , Int ):
58
62
return Int (self .value * other .value )
63
+ elif isinstance (other , Float ):
64
+ return Float (self .value * other .value )
59
65
raise TypeError ()
60
66
61
67
def __truediv__ (self , other ):
62
68
if isinstance (other , Int ):
63
- if self .value % other .value == 0 :
64
- return Int ( self . value // other . value )
69
+ return Float ( self .value / other .value )
70
+ elif isinstance ( other , Float ):
65
71
return Float (self .value / other .value )
66
72
raise TypeError ()
67
73
@@ -74,22 +80,22 @@ def __init__(self, value):
74
80
super ().__init__ (float (value ))
75
81
76
82
def __add__ (self , other ):
77
- if isinstance (other , Float ):
83
+ if isinstance (other , Float ) or isinstance ( other , Int ) :
78
84
return Float (self .value + other .value )
79
85
raise TypeError ()
80
86
81
87
def __sub__ (self , other ):
82
- if isinstance (other , Float ):
88
+ if isinstance (other , Float ) or isinstance ( other , Int ) :
83
89
return Float (self .value - other .value )
84
90
raise TypeError ()
85
91
86
92
def __mul__ (self , other ):
87
- if isinstance (other , Float ):
93
+ if isinstance (other , Float ) or isinstance ( other , Int ) :
88
94
return Float (self .value * other .value )
89
95
raise TypeError ()
90
96
91
97
def __truediv__ (self , other ):
92
- if isinstance (other , Float ):
98
+ if isinstance (other , Float ) or isinstance ( other , Int ) :
93
99
return Float (self .value / other .value )
94
100
raise TypeError ()
95
101
@@ -101,6 +107,16 @@ class String(Value):
101
107
def __init__ (self , value ):
102
108
super ().__init__ (value )
103
109
110
+ def __add__ (self , other ):
111
+ if isinstance (other , String ):
112
+ return String (self .value + other .value )
113
+ raise TypeError ()
114
+
115
+ def __mul__ (self , other ):
116
+ if isinstance (other , Int ):
117
+ return String (self .value * other .value )
118
+ raise TypeError ()
119
+
104
120
105
121
class Vector (Value ):
106
122
def __init__ (self , value : list ):
@@ -130,49 +146,28 @@ def __init__(self, value: list):
130
146
def __str__ (self ):
131
147
return "[" + ", " .join (str (elem ) for elem in self .value ) + "]"
132
148
133
- def mat_add (self , other ):
149
+ def _mat_op (self , other , op ):
134
150
if isinstance (other , Vector ):
135
151
rows = []
136
152
for elem , other_elem in zip (self .value , other .value ):
137
153
if isinstance (elem , Vector ):
138
- rows .append (elem .mat_add (other_elem ))
154
+ rows .append (elem ._mat_op (other_elem , op ))
139
155
else :
140
- rows .append (elem + other_elem )
156
+ rows .append (op ( elem , other_elem ) )
141
157
return Vector (rows )
142
158
raise TypeError ()
143
159
160
+ def mat_add (self , other ):
161
+ return self ._mat_op (other , lambda x , y : x + y )
162
+
144
163
def mat_sub (self , other ):
145
- if isinstance (other , Vector ):
146
- rows = []
147
- for elem , other_elem in zip (self .value , other .value ):
148
- if isinstance (elem , Vector ):
149
- rows .append (elem .mat_sub (other_elem ))
150
- else :
151
- rows .append (elem - other_elem )
152
- return Vector (rows )
153
- raise TypeError ()
164
+ return self ._mat_op (other , lambda x , y : x - y )
154
165
155
166
def mat_mul (self , other ):
156
- if isinstance (other , Vector ):
157
- rows = []
158
- for elem , other_elem in zip (self .value , other .value ):
159
- if isinstance (elem , Vector ):
160
- rows .append (elem .mat_mul (other_elem ))
161
- else :
162
- rows .append (elem * other_elem )
163
- return Vector (rows )
164
- raise TypeError ()
167
+ return self ._mat_op (other , lambda x , y : x * y )
165
168
166
169
def mat_truediv (self , other ):
167
- if isinstance (other , Vector ):
168
- rows = []
169
- for elem , other_elem in zip (self .value , other .value ):
170
- if isinstance (elem , Vector ):
171
- rows .append (elem .mat_truediv (other_elem ))
172
- else :
173
- rows .append (elem / other_elem )
174
- return Vector (rows )
175
- raise TypeError ()
170
+ return self ._mat_op (other , lambda x , y : x / y )
176
171
177
172
def transpose (self ):
178
173
if len (self .dims ) != 2 :
0 commit comments