From 978844ed759ed798d20c8a2826187e2ee025fa32 Mon Sep 17 00:00:00 2001 From: Loosetooth Date: Sat, 1 Feb 2025 23:56:20 +0100 Subject: [PATCH] add kata --- 5kyu/vector-class/description.md | 18 ++++++++++++++ 5kyu/vector-class/solution.ts | 40 ++++++++++++++++++++++++++++++++ 5kyu/vector-class/tests.ts | 37 +++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 5kyu/vector-class/description.md create mode 100644 5kyu/vector-class/solution.ts create mode 100644 5kyu/vector-class/tests.ts diff --git a/5kyu/vector-class/description.md b/5kyu/vector-class/description.md new file mode 100644 index 0000000..8712401 --- /dev/null +++ b/5kyu/vector-class/description.md @@ -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. \ No newline at end of file diff --git a/5kyu/vector-class/solution.ts b/5kyu/vector-class/solution.ts new file mode 100644 index 0000000..5188433 --- /dev/null +++ b/5kyu/vector-class/solution.ts @@ -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(',')})`; + } +}; \ No newline at end of file diff --git a/5kyu/vector-class/tests.ts b/5kyu/vector-class/tests.ts new file mode 100644 index 0000000..1318744 --- /dev/null +++ b/5kyu/vector-class/tests.ts @@ -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)"); + }) +}); \ No newline at end of file