Back to Freecodecamp

Step 11

curriculum/challenges/english/blocks/learn-special-methods-by-building-a-vector-space/65f3fe07cc763212efe91285.md

latest1.4 KB
Original Source

--description--

A vector can have a number n of dimensions (components). Here's a representation of a 3-dimensional vector:

So far, you created a 2-dimensional vector. You want to be able to represent vectors with a different number of dimensions without rewriting the necessary code for each specific case. For that, you will use inheritance.

Start by renaming the Vector class into R2Vector to specify that this class is going to deal with 2-dimensional vectors. Remember to modify the instantiation of v1, too.

--hints--

You should rename the Vector class into R2Vector. Remember to modify the instantiation of v1, too.

js
({
    test: () => assert(runPython(`
      _Node(_code).has_class("R2Vector")
    `))
})

--seed--

--seed-contents--

py
--fcc-editable-region--
class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
    def norm(self):
        return (self.x**2 + self.y**2)**0.5
        
    def __str__(self):
        return f'{self.x, self.y}'

v1 = Vector(2, 3)
print(v1.norm())
print(v1)
--fcc-editable-region--