5 kyu
Vector class
3,313 of 8,805eugene-bulkin
Description:
Create a Vector object that supports addition, subtraction, dot products, and norms. So, for example:
var a = new Vector([1, 2, 3]);
var b = new Vector([3, 4, 5]);
var c = new Vector([5, 6, 7, 8]);
a.add(b); // should return a new Vector([4, 6, 8])
a.subtract(b); // should return a new Vector([-2, -2, -2])
a.dot(b); // should return 1*3 + 2*4 + 3*5 = 26
a.norm(); // should return sqrt(1^2 + 2^2 + 3^2) = sqrt(14)
a.add(c); // throws an error
If you try to add, subtract, or dot two vectors with different lengths, you must throw an error!
Also provide:
- a
toString
method, so that using the vectors from above,a.toString() === '(1,2,3)'
(in Python, this is a__str__
method, so thatstr(a) == '(1,2,3)'
) - an
equals
method, to check that two vectors that have the same components are equal
Note: the test cases will utilize the user-provided equals
method.
Object-oriented Programming
Algorithms
Linear Algebra
Similar Kata:
Stats:
Created | Oct 28, 2013 |
Published | Oct 28, 2013 |
Warriors Trained | 23098 |
Total Skips | 5878 |
Total Code Submissions | 98480 |
Total Times Completed | 8805 |
JavaScript Completions | 3313 |
CoffeeScript Completions | 164 |
Python Completions | 4820 |
TypeScript Completions | 728 |
Total Stars | 628 |
% of votes with a positive feedback rating | 87% of 915 |
Total "Very Satisfied" Votes | 721 |
Total "Somewhat Satisfied" Votes | 156 |
Total "Not Satisfied" Votes | 38 |