Tell us what’s happening:
Either I do not understand the request or do not know how how to implement this to satisfy the code check.
help!
v1 = (2, 3)
v2 = (0.5, 1.25)
v3 = v1 * v2
Your code so far
class R2Vector:
def __init__(self, *, x, y):
self.x = x
self.y = y
def norm(self):
return sum(val**2 for val in vars(self).values())**0.5
def __str__(self):
return str(tuple(getattr(self, i) for i in vars(self)))
def __repr__(self):
arg_list = [f'{key}={val}' for key, val in vars(self).items()]
args = ', '.join(arg_list)
return f'{self.__class__.__name__}({args})'
def __add__(self, other):
if type(self) != type(other):
return NotImplemented
kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
return self.__class__(**kwargs)
def __sub__(self, other):
if type(self) != type(other):
return NotImplemented
kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
return self.__class__(**kwargs)
# User Editable Region
def __mul__(self, other):
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
print( {i: getattr(self, i) * getattr(other, i) for i in vars(self)} )
kwargs = 0
for l in {getattr(self, i) * getattr(other, i) for i in vars(self)} :
kwargs+= l
print(kwargs)
return self.__class__(**kwargs)
# User Editable Region
class R3Vector(R2Vector):
def __init__(self, *, x, y, z):
super().__init__(x=x, y=y)
self.z = z
using the following:
v1 = (2, 3)
v2 = (0.5, 1.25)
v3 = v1 * v2
The code below returns a valid tuple - {‘x’: 1.0, ‘y’: 3.75}. Is a valid overload of multiply with a valid return.
Works when I create and multiple a new vector object in a series (v1 * v2 * v3)
def __mul__(self, other):
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
kwargs = ( {i: getattr(self, i) * getattr(other, i) for i in vars(self)} )
return self.__class__(**kwargs)
Conversly the following is my implemenation of scalar product.
(2 .05) + (3 1.25) = 1.0 + 3.75 = 4.75
Returns the correct product 4.75.
The code returns the error - typeError: main.R2Vector() argument after ** must be a mapping, not float
def __mul__(self, other):
if type(other) in (int, float):
kwargs = {i: getattr(self, i) * other for i in vars(self)}
return self.__class__(**kwargs)
elif type(self) == type(other):
for l in {getattr(self, i) * getattr(other, i) for i in vars(self)} ):
kwargs+= l
return self.__class__(**kwargs)
type or paste code here
Not sure how to resolve the request and pass the ‘Check your Code’.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Learn Special Methods by Building a Vector Space - Step 54
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-special-methods-by-building-a-vector-space/step-54`Preformatted text`