challenge ctci-random-note

This commit is contained in:
Lorenzo Iovino 2024-01-25 18:44:05 +01:00
parent 3b7d0b4574
commit 4ef3e01ab5
5 changed files with 61 additions and 2 deletions

View file

@ -9,7 +9,9 @@
"build+start": "npm run build && npm run start",
"build:watch": "tsc -w -p tsconfig.json",
"test:watch": "node --import tsx --test --watch **/**/*.spec.ts",
"test": "node --import tsx --test **/**/*.spec.ts"
"test:watch:only": "node --import tsx --test --test-only --watch **/**/*.spec.ts",
"test": "node --import tsx --test **/**/*.spec.ts",
"test:only": "node --import tsx --test --test-only **/**/*.spec.ts"
},
"author": "",
"license": "ISC",

View file

@ -2,7 +2,7 @@ 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("2D Array", () => {
describe("findHighestHourglassSum", () => {
it("should return 0 if the matrix is empty", () => {
assert.equal(findHighestHourglassSum(new Matrix([])), 0);

View file

@ -0,0 +1,36 @@
import {describe, it} from "node:test";
import assert = require("node:assert");
import {checkMagazine} from "./ctci-ransom-note";
describe("Ctci Ransom Note", () => {
describe("checkMagazine", () => {
it("if note is empty should return Yes", () => {
assert.deepStrictEqual(checkMagazine(["bla", "ble", "bli"], []), "Yes");
})
it("if magazine is empty and note have words should return No", () => {
assert.deepStrictEqual(checkMagazine([], ["bla", "ble", "bli"]), "No");
})
it("Yes case", () => {
assert.deepStrictEqual(checkMagazine(["give", "me", "one", "grand" ,"today" ,"night"], ["give", "one", "grand", "today"]), "Yes");
})
it("No case due case-sensitive in magazines", () => {
assert.deepStrictEqual(checkMagazine(["give", "me", "one", "Grand" ,"today" ,"night"], ["give", "one", "grand", "today"]), "No");
})
it("No case due case-sensitive in note", () => {
assert.deepStrictEqual(checkMagazine(["give", "me", "one", "grand" ,"today" ,"night"], ["give", "one", "Grand", "today"]), "No");
})
it("No case for lack of duplicated words", () => {
assert.deepStrictEqual(checkMagazine(["two", "times", "three", "is" ,"not" ,"four"], ["two", "times", "two", "is", "four"]), "No");
})
it("No case", () => {
assert.deepStrictEqual(checkMagazine(["ive", "got", "a", "lovely" ,"bunch" ,"of", "coconuts"], ["ive", "got", "some", "coconuts"]), "No");
})
});
})

View file

@ -0,0 +1,21 @@
export function checkMagazine(magazine: Array<string>, note: Array<string>): 'Yes' | 'No' {
if(magazine.length === 0 && note.length > 0) {
return 'No'
}
if(note.length === 0) {
return 'Yes'
}
for(const n of note) {
const index = magazine.indexOf(n);
if(index > -1) {
magazine.splice(index, 1)
} else {
return 'No'
}
}
return 'Yes'
}

View file