challenge 2d-array
This commit is contained in:
parent
10d13dc384
commit
069cfb45fe
7 changed files with 229 additions and 41 deletions
23
src/2d-array/2d-array.spec.ts
Normal file
23
src/2d-array/2d-array.spec.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import {describe, it, skip} from 'node:test';
|
||||||
|
import * as assert from "node:assert";
|
||||||
|
import {findHighestHourglassSum} from "./2d-array";
|
||||||
|
import {Matrix} from "./matrix";
|
||||||
|
describe("2D Array", () => {
|
||||||
|
describe("findHighestHourglassSum", () => {
|
||||||
|
it("should return 0 if the matrix is empty", () => {
|
||||||
|
assert.equal(findHighestHourglassSum(new Matrix([])), 0);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 19 with this matrix", () => {
|
||||||
|
assert.equal(findHighestHourglassSum(
|
||||||
|
new Matrix([
|
||||||
|
[1, 1, 1, 0, 0, 0],
|
||||||
|
[0, 1, 0, 0, 0, 0],
|
||||||
|
[1, 1, 1, 0, 0, 0],
|
||||||
|
[0, 0, 2, 4, 4, 0],
|
||||||
|
[0, 0, 0, 2, 0, 0],
|
||||||
|
[0, 0, 1, 2, 4, 0]]
|
||||||
|
)), 19);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
18
src/2d-array/2d-array.ts
Normal file
18
src/2d-array/2d-array.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import {Matrix} from "./matrix";
|
||||||
|
import {Hourglass} from "./hourglass";
|
||||||
|
|
||||||
|
export type TwoDimensionVector = Array<number[]>
|
||||||
|
|
||||||
|
export function findHighestHourglassSum(matrix: Matrix) {
|
||||||
|
const hourglasses: Hourglass[] = Hourglass.extractFromMatrix(matrix);
|
||||||
|
const sums: number[] = [];
|
||||||
|
|
||||||
|
if(hourglasses.length === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for(let i = 0; i < hourglasses.length; i++) {
|
||||||
|
sums.push(hourglasses[i].sum());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.max(...sums);
|
||||||
|
}
|
||||||
76
src/2d-array/hourglass.spec.ts
Normal file
76
src/2d-array/hourglass.spec.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
import {describe, it, skip} from 'node:test';
|
||||||
|
import * as assert from "node:assert";
|
||||||
|
import {Hourglass} from "./hourglass";
|
||||||
|
import {Matrix} from "./matrix";
|
||||||
|
import {TwoDimensionVector} from "./2d-array";
|
||||||
|
describe("2D Array", () => {
|
||||||
|
describe("extractFromMatrix", () => {
|
||||||
|
it("should extract the hourglasses matrices", () => {
|
||||||
|
const hourglasses = Hourglass.extractFromMatrix(new Matrix([
|
||||||
|
[1, 1, 1, 0, 0, 0],
|
||||||
|
[0, 1, 0, 0, 0, 0],
|
||||||
|
[1, 1, 1, 0, 0, 0],
|
||||||
|
[0, 0, 2, 4, 4, 0],
|
||||||
|
[0, 0, 0, 2, 0, 0],
|
||||||
|
[0, 0, 1, 2, 4, 0]
|
||||||
|
]));
|
||||||
|
|
||||||
|
const matrixOfHourglasses: TwoDimensionVector[] = [];
|
||||||
|
hourglasses.forEach(hG => {
|
||||||
|
matrixOfHourglasses.push(hG.items);
|
||||||
|
})
|
||||||
|
assert.deepStrictEqual(matrixOfHourglasses, [
|
||||||
|
[[1, 1, 1], [0, 1, 0], [1, 1, 1]],
|
||||||
|
[[1, 1, 0], [0, 0, 0], [1, 1, 0]],
|
||||||
|
[[1, 0, 0], [0, 0, 0], [1, 0, 0]],
|
||||||
|
[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
|
||||||
|
|
||||||
|
[[0, 1, 0], [0, 1, 0], [0, 0, 2]],
|
||||||
|
[[1, 0, 0], [0, 1, 0], [0, 2, 4]],
|
||||||
|
[[0, 0, 0], [0, 0, 0], [2, 4, 4]],
|
||||||
|
[[0, 0, 0], [0, 0, 0], [4, 4, 0]],
|
||||||
|
|
||||||
|
[[1, 1, 1], [0, 0, 0], [0, 0, 0]],
|
||||||
|
[[1, 1, 0], [0, 2, 0], [0, 0, 2]],
|
||||||
|
[[1, 0, 0], [0, 4, 0], [0, 2, 0]],
|
||||||
|
[[0, 0, 0], [0, 4, 0], [2, 0, 0]],
|
||||||
|
|
||||||
|
[[0, 0, 2], [0, 0, 0], [0, 0, 1]],
|
||||||
|
[[0, 2, 4], [0, 0, 0], [0, 1, 2]],
|
||||||
|
[[2, 4, 4], [0, 2, 0], [1, 2, 4]],
|
||||||
|
[[4, 4, 0], [0, 0, 0], [2, 4, 0]],
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return a single hourglass", () => {
|
||||||
|
const hourglasses = Hourglass.extractFromMatrix(
|
||||||
|
(new Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]
|
||||||
|
])));
|
||||||
|
|
||||||
|
assert.equal(hourglasses.length, 1)
|
||||||
|
assert.deepStrictEqual(hourglasses[0].items, [[1, 1, 1], [0, 1, 0], [1, 1, 1]])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return zero hourglass", () => {
|
||||||
|
assert.deepStrictEqual(Hourglass.extractFromMatrix(
|
||||||
|
new Matrix([])), [])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return zero hourglass", () => {
|
||||||
|
assert.deepStrictEqual(Hourglass.extractFromMatrix(
|
||||||
|
new Matrix([[1, 2], [3, 4]])), [])
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("sum", () => {
|
||||||
|
it("should return the sum of every item in an hourglass", () => {
|
||||||
|
assert.equal(new Hourglass([[1, 1, 1], [1, 1, 1], [1, 1, 1]]).sum(),7);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("hourglassify", () => {
|
||||||
|
it("should convert a 3x3 matrix in an hourglass", () => {
|
||||||
|
assert.deepStrictEqual(Hourglass.hourglassify([[1, 1, 1], [1, 1, 1], [1, 1, 1]]),[[1, 1, 1], [0, 1, 0], [1, 1, 1]]);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
56
src/2d-array/hourglass.ts
Normal file
56
src/2d-array/hourglass.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
import {TwoDimensionVector} from "./2d-array";
|
||||||
|
import {Matrix} from "./matrix";
|
||||||
|
/**
|
||||||
|
* An hourglass is a matrix of numbers with this shape:
|
||||||
|
* a b c x x x
|
||||||
|
* 0 d 0 0 x 0
|
||||||
|
* e f g x x x
|
||||||
|
*/
|
||||||
|
|
||||||
|
export class Hourglass {
|
||||||
|
items: TwoDimensionVector = []
|
||||||
|
|
||||||
|
constructor(items: TwoDimensionVector) {
|
||||||
|
this.items = Hourglass.hourglassify(items);
|
||||||
|
}
|
||||||
|
|
||||||
|
static hourglassify(items: TwoDimensionVector): TwoDimensionVector {
|
||||||
|
items[1][0] = 0;
|
||||||
|
items[1][2] = 0;
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
sum(): number {
|
||||||
|
let sum = 0;
|
||||||
|
this.items.forEach(item => {
|
||||||
|
item.forEach(num => {
|
||||||
|
sum += num;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
static extractFromMatrix(matrix: Matrix): Hourglass[] {
|
||||||
|
if (matrix.items.length >= 3) {
|
||||||
|
const hourglasses: Hourglass[] = [];
|
||||||
|
let hourglass: Hourglass;
|
||||||
|
const width = matrix.items[0].length;
|
||||||
|
const height = matrix.items.length;
|
||||||
|
|
||||||
|
const maxRightShift = width - 3;
|
||||||
|
const maxTopShift = height - 3;
|
||||||
|
|
||||||
|
for(let tS = 0; tS <= maxTopShift; tS++) {
|
||||||
|
for (let rS = 0; rS <= maxRightShift; rS++) {
|
||||||
|
hourglasses.push(new Hourglass([
|
||||||
|
[matrix.items[tS][rS], matrix.items[tS][rS + 1], matrix.items[tS][rS + 2]],
|
||||||
|
[matrix.items[tS + 1][rS], matrix.items[tS + 1][rS + 1], matrix.items[tS + 1][rS + 2]],
|
||||||
|
[matrix.items[tS + 2][rS], matrix.items[tS + 2][rS + 1], matrix.items[tS + 2][rS + 2]]]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hourglasses;
|
||||||
|
} else {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/2d-array/matrix.spec.ts
Normal file
4
src/2d-array/matrix.spec.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import {describe, it, skip} from 'node:test';
|
||||||
|
describe("Matrix", () => {
|
||||||
|
skip();
|
||||||
|
})
|
||||||
9
src/2d-array/matrix.ts
Normal file
9
src/2d-array/matrix.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import {TwoDimensionVector} from "./2d-array";
|
||||||
|
|
||||||
|
export class Matrix {
|
||||||
|
items: TwoDimensionVector = []
|
||||||
|
|
||||||
|
constructor(items: TwoDimensionVector) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,50 +2,52 @@ import {describe, it} from 'node:test';
|
||||||
import * as assert from "node:assert";
|
import * as assert from "node:assert";
|
||||||
import {countA, countInstanceOfCharInAString} from "./repeated-string";
|
import {countA, countInstanceOfCharInAString} from "./repeated-string";
|
||||||
|
|
||||||
describe("countA", () => {
|
describe("Repeated String", () => {
|
||||||
it("should return 0 if the s string is empty" , (t) => {
|
describe("countA", () => {
|
||||||
assert.equal(countA("", 0), 0);
|
it("should return 0 if the s string is empty", (t) => {
|
||||||
|
assert.equal(countA("", 0), 0);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 0 if the n is zero", (t) => {
|
||||||
|
assert.equal(countA("", 0), 0);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 1 if the string is 'a' and n is 1", (t) => {
|
||||||
|
assert.equal(countA("a", 1), 1);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 2 if the string is 'a' and n is 2", (t) => {
|
||||||
|
assert.equal(countA("a", 2), 2);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 1 if the string is 'acab' and n is 2", (t) => {
|
||||||
|
assert.equal(countA("acab", 2), 1);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 4 if the string is 'abcac' and n is 10", (t) => {
|
||||||
|
assert.equal(countA("abcac", 10), 4);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return 2 if the string is 'ababa' and n is 3", (t) => {
|
||||||
|
assert.equal(countA("ababa", 3), 2)
|
||||||
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should return 0 if the n is zero" , (t) => {
|
describe("countInstanceOfCharInAString", () => {
|
||||||
assert.equal(countA("", 0), 0);
|
it("should return 0 if the s string is empty", (t) => {
|
||||||
})
|
assert.equal(countInstanceOfCharInAString("a", ""), 0);
|
||||||
|
})
|
||||||
|
|
||||||
it("should return 1 if the string is 'a' and n is 1", (t) => {
|
it("should return 0 if the char is empty", (t) => {
|
||||||
assert.equal(countA("a", 1), 1);
|
assert.equal(countInstanceOfCharInAString("", "hola"), 0);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should return 2 if the string is 'a' and n is 2", (t) => {
|
it("should return 0 if the char is empty and the string is empty", (t) => {
|
||||||
assert.equal(countA("a", 2), 2);
|
assert.equal(countInstanceOfCharInAString("", ""), 0);
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should return 1 if the string is 'acab' and n is 2", (t) => {
|
it("should return 3 if the char is 'a' and the string is 'aaa'", (t) => {
|
||||||
assert.equal(countA("acab", 2), 1);
|
assert.equal(countInstanceOfCharInAString("a", "aaa"), 3);
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
});
|
||||||
it("should return 4 if the string is 'abcac' and n is 10", (t) => {
|
|
||||||
assert.equal(countA("abcac", 10), 4);
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return 2 if the string is 'ababa' and n is 3", (t) => {
|
|
||||||
assert.equal(countA("ababa", 3), 2)
|
|
||||||
});
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("countInstanceOfCharInAString", () => {
|
|
||||||
it("should return 0 if the s string is empty" , (t) => {
|
|
||||||
assert.equal(countInstanceOfCharInAString("a", ""), 0);
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return 0 if the char is empty" , (t) => {
|
|
||||||
assert.equal(countInstanceOfCharInAString("", "hola"), 0);
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return 0 if the char is empty and the string is empty" , (t) => {
|
|
||||||
assert.equal(countInstanceOfCharInAString("", ""), 0);
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return 3 if the char is 'a' and the string is 'aaa'" , (t) => {
|
|
||||||
assert.equal(countInstanceOfCharInAString("a", "aaa"), 3);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue