@@ -46,22 +46,28 @@ def __init__(self, value):
4646 def __add__ (self , other ):
4747 if isinstance (other , Int ):
4848 return Int (self .value + other .value )
49+ elif isinstance (other , Float ):
50+ return Float (self .value + other .value )
4951 raise TypeError ()
5052
5153 def __sub__ (self , other ):
5254 if isinstance (other , Int ):
5355 return Int (self .value - other .value )
56+ elif isinstance (other , Float ):
57+ return Float (self .value - other .value )
5458 raise TypeError ()
5559
5660 def __mul__ (self , other ):
5761 if isinstance (other , Int ):
5862 return Int (self .value * other .value )
63+ elif isinstance (other , Float ):
64+ return Float (self .value * other .value )
5965 raise TypeError ()
6066
6167 def __truediv__ (self , other ):
6268 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 ):
6571 return Float (self .value / other .value )
6672 raise TypeError ()
6773
@@ -74,22 +80,22 @@ def __init__(self, value):
7480 super ().__init__ (float (value ))
7581
7682 def __add__ (self , other ):
77- if isinstance (other , Float ):
83+ if isinstance (other , Float ) or isinstance ( other , Int ) :
7884 return Float (self .value + other .value )
7985 raise TypeError ()
8086
8187 def __sub__ (self , other ):
82- if isinstance (other , Float ):
88+ if isinstance (other , Float ) or isinstance ( other , Int ) :
8389 return Float (self .value - other .value )
8490 raise TypeError ()
8591
8692 def __mul__ (self , other ):
87- if isinstance (other , Float ):
93+ if isinstance (other , Float ) or isinstance ( other , Int ) :
8894 return Float (self .value * other .value )
8995 raise TypeError ()
9096
9197 def __truediv__ (self , other ):
92- if isinstance (other , Float ):
98+ if isinstance (other , Float ) or isinstance ( other , Int ) :
9399 return Float (self .value / other .value )
94100 raise TypeError ()
95101
@@ -101,6 +107,16 @@ class String(Value):
101107 def __init__ (self , value ):
102108 super ().__init__ (value )
103109
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+
104120
105121class Vector (Value ):
106122 def __init__ (self , value : list ):
@@ -130,49 +146,28 @@ def __init__(self, value: list):
130146 def __str__ (self ):
131147 return "[" + ", " .join (str (elem ) for elem in self .value ) + "]"
132148
133- def mat_add (self , other ):
149+ def _mat_op (self , other , op ):
134150 if isinstance (other , Vector ):
135151 rows = []
136152 for elem , other_elem in zip (self .value , other .value ):
137153 if isinstance (elem , Vector ):
138- rows .append (elem .mat_add (other_elem ))
154+ rows .append (elem ._mat_op (other_elem , op ))
139155 else :
140- rows .append (elem + other_elem )
156+ rows .append (op ( elem , other_elem ) )
141157 return Vector (rows )
142158 raise TypeError ()
143159
160+ def mat_add (self , other ):
161+ return self ._mat_op (other , lambda x , y : x + y )
162+
144163 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 )
154165
155166 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 )
165168
166169 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 )
176171
177172 def transpose (self ):
178173 if len (self .dims ) != 2 :
0 commit comments