Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ DOCKER_COMPOSE=docker compose
.PHONY: all clean dependencies help list test outdated
.EXPORT_ALL_VARIABLES: # (2)

define crono
@start=$$(date +%s); \
$(1); \
end=$$(date +%s); \
diff=$$((end - start)); \
printf "Total time: %02d:%02d:%02d\n" $$((diff/3600)) $$((diff%3600/60)) $$((diff%60))
endef


help: list

list:
Expand Down Expand Up @@ -135,7 +144,8 @@ compose/test: compose/build
compose/run: compose/build
${DOCKER_COMPOSE} --profile production run --rm algorithm-exercises-js make run

all: env dependencies test
all:
$(call crono, make clean && make dependencies && make build && make test && make lint && make coverage/html)

run:
ls -alh
4 changes: 2 additions & 2 deletions src/hackerrank/implementation/countingValleys.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function countingValleys(steps, path) {

console.debug(stepList);

stepList.forEach((step) => {
for (const step of stepList) {
if (step === 'D') {
if (altitude === 0) {
valleys += 1;
Expand All @@ -21,7 +21,7 @@ function countingValleys(steps, path) {
if (step === 'U') {
altitude += 1;
}
});
}

return valleys;
}
Expand Down
8 changes: 4 additions & 4 deletions src/hackerrank/implementation/migratoryBirds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @link Problem definition [[docs/hackerrank/implementation/migratoryBirds.md]]
*/

import util from 'util';
import util from 'node:util';
import { logger as console } from '../../logger.js';

function migratoryBirds(arr) {
Expand All @@ -16,10 +16,10 @@ function migratoryBirds(arr) {
for (const bird of arr) {
console.debug(`bird ${bird}`);

if (!map[bird]) {
map[bird] = 1;
} else {
if (map[bird]) {
map[bird] += 1;
} else {
map[bird] = 1;
}

console.debug(`bird = ${bird} ~> map[bird] = ${map[bird]}`);
Expand Down
4 changes: 2 additions & 2 deletions src/hackerrank/implementation/repeatedString.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ function countAs(word) {

const chars = word.split('');

chars.forEach((char) => {
for (const char of chars) {
if (char === 'a') {
result += 1;
}
});
}

return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hackerrank/implementation/sockMerchant.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function sockMerchant(n, ar) {

const matches = {};

ar.forEach((v) => {
for (const v of ar) {
matches[v] = matches?.[v] ? matches[v] + 1 : 1;
});
}

console.debug(matches);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe('arrays: 2d Array hourglassSum', () => {
it('hourglassSum Test Cases', () => {
expect.assertions(4);

TEST_CASES.forEach((test) => {
for (const test of TEST_CASES) {
const answer = hourglassSum(test.input);

console.debug(
`gethourGlass(${test.input.toString()}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(TEST_CASES).toHaveLength(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ function arrayManipulation(n, queries) {
const result = Array(LENGTH).fill(SURROGATE_VALUE);
let maximum = 0;

queries.forEach((query) => {
for (const query of queries) {
const [aStart, bEnd, kValue] = query;
console.debug(`start -> ${result}`);

for (let i = aStart; i <= bEnd; i++) {
result[i] += kValue;
console.debug(`result -> ${result}`);
}
});
}

result.forEach((value) => {
for (const value of result) {
maximum = Math.max(value, maximum);
});
}

return maximum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ describe('arrays: crush (bruteforce) small cases', () => {
it('arrayManipulation Test Cases', () => {
expect.assertions(4);

TEST_CASES.forEach((test) => {
for (const test of TEST_CASES) {
const answer = arrayManipulation(test.n, test.queries);

console.debug(
`arrayManipulation(${test.n}, ${test.queries}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(TEST_CASES).toHaveLength(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ function arrayManipulation(n, queries) {
const result = Array(LENGTH).fill(INITIAL_VALUE);
let maximum = 0;

queries.forEach((query) => {
for (const query of queries) {
const [aStart, bEnd, kValue] = query;

result[aStart] += kValue;
result[bEnd + 1] -= kValue;
});
}

let accumSum = 0;

result.forEach((value) => {
for (const value of result) {
accumSum += value;
maximum = Math.max(maximum, accumSum);
});
}

return maximum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ describe('arrays: crush (optimized)', () => {
it('arrayManipulation Test Cases', () => {
expect.assertions(4);

TEST_CASES.forEach((test) => {
for (const test of TEST_CASES) {
const answer = arrayManipulation(test.n, test.queries);

console.debug(
`arrayManipulation(${test.n}, ${test.queries}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(TEST_CASES).toHaveLength(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe('ctci_array_left_rotation', () => {
it('rotLeft Test cases', () => {
expect.assertions(9);

ROT_LEFT_TEST_CASES.forEach((test) => {
for (const test of ROT_LEFT_TEST_CASES) {
const answer = rotLeft(test.input, test.d_rotations);

console.debug(`rotLeft(${test.numbers}) solution found: ${answer}`);

expect(answer).toStrictEqual(test.expected);
});
}

expect(ROT_LEFT_TEST_CASES).toHaveLength(8);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ describe('minimum swaps 2', () => {
it('minimumSwaps', () => {
expect.assertions(4);

TEST_CASES.forEach((test) => {
for (const test of TEST_CASES) {
const answer = minimumSwaps(test.input);

console.debug(`minimumSwaps(${test.input}) solution found: ${answer}`);

expect(answer).toStrictEqual(test.expected);
});
}

expect(TEST_CASES).toHaveLength(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function minimumBribesCalculate(q) {
let bribes = 0;
let i = 0;

q.forEach((value) => {
for (const value of q) {
const position = i + 1;
if (value - position > NEW_YEAR_CHAOS_TOLERANCE) {
throw new Error(TOO_CHAOTIC_ERROR);
Expand All @@ -20,13 +20,13 @@ function minimumBribesCalculate(q) {
i
);

fragment.forEach((k) => {
for (const k of fragment) {
if (k > value) {
bribes += 1;
}
});
}
i += 1;
});
}

return bribes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('new_year_chaos', () => {
it('minimumBribes Test Cases', () => {
expect.assertions(6);

TEST_CASES.forEach((test) => {
for (const test of TEST_CASES) {
const answer = minimumBribesText(test.input);
minimumBribes(test.input);

Expand All @@ -18,7 +18,7 @@ describe('new_year_chaos', () => {
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(TEST_CASES).toHaveLength(5);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ describe('count_triplets_1', () => {
it('countTriplets test cases', () => {
expect.assertions(5);

SMALL_TEST_CASES.forEach((test) => {
for (const test of SMALL_TEST_CASES) {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(SMALL_TEST_CASES).toHaveLength(4);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ describe('count_triplets_1 (optimized)', () => {
it('countTriplets small test cases', () => {
expect.assertions(5);

SMALL_TEST_CASES.forEach((test) => {
for (const test of SMALL_TEST_CASES) {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(SMALL_TEST_CASES).toHaveLength(4);
});

it('countTriplets big test cases', () => {
expect.assertions(2);

BIG_TEST_CASES.forEach((test) => {
for (const test of BIG_TEST_CASES) {
const answer = countTriplets(test.input, test.r);

console.debug(
`countTriplets(${test.input}, ${test.r}) solution found: ${answer}`
);

expect(answer).toStrictEqual(test.expected);
});
}

expect(BIG_TEST_CASES).toHaveLength(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function countTriplets(arr, ratio) {

const bCounter = {};

arr.forEach((x) => {
for (const x of arr) {
const j = Math.floor(x / ratio);
const k = x * ratio;
aCounter[x] -= 1;
Expand All @@ -30,7 +30,7 @@ function countTriplets(arr, ratio) {
} else {
bCounter[x] = 1;
}
});
}

return triplets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('ctci_ransom_note', () => {
it('checkMagazine test cases', () => {
expect.assertions(4);

TEST_CASES.forEach((value) => {
for (const value of TEST_CASES) {
const answer = checkMagazineText(value.magazine, value.note);
checkMagazine(value.magazine, value.note);

Expand All @@ -17,7 +17,7 @@ describe('ctci_ransom_note', () => {
);

expect(answer).toStrictEqual(value.expected);
});
}

expect(TEST_CASES).toHaveLength(3);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function freqQuery(queries) {
const __NOT_FOUND__ = 0;
const __FOUND__ = 1;

queries.forEach((query) => {
for (const query of queries) {
const [operation, data] = query;

const current = dataMap?.[data] ?? __INITIAL__;
Expand All @@ -39,7 +39,7 @@ function freqQuery(queries) {
default:
throw new Error('Invalid operation');
}
});
}

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ describe('frequency_queries', () => {
it('freqQuery test cases', () => {
expect.assertions(5);

SMALL_TEST_CASES.forEach((value) => {
for (const value of SMALL_TEST_CASES) {
const answer = freqQuery(value.input);

console.debug(`freqQuery(${value.input}) solution found: ${answer}`);

expect(answer).toStrictEqual(value.expected);
});
}

expect(SMALL_TEST_CASES).toHaveLength(4);
});
Expand Down
Loading