This commit is contained in:
2025-02-01 23:56:20 +01:00
parent 2dab36b36e
commit 978844ed75
3 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
Create a Vector object that supports addition, subtraction, dot products, and norms. So, for example:
a = new Vector([1, 2, 3])
b = new Vector([3, 4, 5])
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 that str(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.

View File

@@ -0,0 +1,40 @@
export class Vector {
private elements: number[];
constructor(components: number[]) {
this.elements = components;
}
public add(vector: Vector): Vector {
if (vector.elements.length !== this.elements.length) {
throw new Error('Vectors must have the same length');
}
return new Vector(this.elements.map((element, i) => element + vector.elements[i]));
}
public subtract(vector: Vector): Vector {
if (vector.elements.length !== this.elements.length) {
throw new Error('Vectors must have the same length');
}
return new Vector(this.elements.map((element, i) => element - vector.elements[i]));
}
public dot(vector: Vector): number {
if (vector.elements.length !== this.elements.length) {
throw new Error('Vectors must have the same length');
}
return this.elements.reduce((sum, element, i) => sum + element * vector.elements[i], 0);
}
public norm(): number {
return Math.sqrt(this.elements.reduce((sum, element) => sum + element ** 2, 0));
}
public equals(vector: Vector): boolean {
return this.elements.every((element, i) => element === vector.elements[i]);
}
public toString(): string {
return `(${this.elements.join(',')})`;
}
};

View File

@@ -0,0 +1,37 @@
import { assert } from "chai";
import { Vector } from "./solution";
describe("Tests", () => {
it("Simple Equality Test", () => {
let a = new Vector([1,2]);
let b = new Vector([3,4]);
assert.isFalse(a.equals(b));
});
it("Simple Add Test", function() {
let a = new Vector([1, 2, 3]);
let b = new Vector([3, 4, 5]);
assert.isTrue(a.add(b).equals(new Vector([4,6, 8])));
})
it("Simple Norm Test", function () {
assert.approximately(new Vector([1,2,3]).norm(), Math.sqrt(14), 0.1);
});
it("Simple Subtract Test", function() {
let a = new Vector([1, 2, 3]);
let b = new Vector([3, 4, 5]);
assert.isTrue(a.subtract(b).equals(new Vector([-2,-2,-2])));
});
it("Simple Dot Test", function() {
let a = new Vector([1, 2, 3]);
let b = new Vector([3, 4, 5]);
assert.strictEqual(a.dot(b), 26);
})
it("Simple ToString Test", function() {
let a = new Vector([1, 2, 3]);
assert.strictEqual(a.toString(), "(1,2,3)");
})
});