Skip to content

Commit d69213b

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Hash Tables: Ice Cream Parlor. Brute force solution added.
1 parent 5986eb0 commit d69213b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import {
5+
whatFlavors,
6+
what_flavors_bruteforce_compute
7+
} from './ctci_ice_cream_parlor_bruteforce';
8+
import TEST_CASES from './ctci_ice_cream_parlor.testcases.json';
9+
import TEST_CASES_BORDER_CASES from './ctci_ice_cream_parlor.border_testcases.json';
10+
11+
describe('ctci_ice_cream_parlor_bruteforce', () => {
12+
it('whatFlavors test cases', () => {
13+
expect.assertions(10);
14+
15+
TEST_CASES.forEach((testSet) => {
16+
testSet?.tests.forEach((test) => {
17+
const answer = what_flavors_bruteforce_compute(test.costs, test.money);
18+
19+
console.debug(
20+
`${testSet.title} ctci_ice_cream_parlor_bruteforce(${test.costs}, ${test.money}) solution found: ${answer}`
21+
);
22+
23+
expect(answer).toStrictEqual(test.expected);
24+
expect(whatFlavors(test.costs, test.money)).toBeUndefined();
25+
});
26+
});
27+
});
28+
29+
it('whatFlavors border test cases', () => {
30+
expect.assertions(2);
31+
32+
TEST_CASES_BORDER_CASES.forEach((testSet) => {
33+
testSet?.tests.forEach((test) => {
34+
expect(
35+
what_flavors_bruteforce_compute(test.costs, test.money)
36+
).toStrictEqual(test.expected);
37+
expect(whatFlavors(test.costs, test.money)).toBeUndefined();
38+
});
39+
});
40+
});
41+
42+
it('whatFlavors test caller function', () => {
43+
expect.assertions(1);
44+
45+
const cost: number[] = [];
46+
const money: number = 100;
47+
48+
expect(whatFlavors(cost, money)).toBeUndefined();
49+
});
50+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/search/ctci-ice-cream-parlor.md]]
3+
*/
4+
5+
export function what_flavors_bruteforce_compute(
6+
cost: number[],
7+
money: number
8+
): number[] {
9+
for (const _i in cost) {
10+
const i: number = parseInt(_i);
11+
const x: number = cost[i];
12+
13+
const budget = money - x;
14+
15+
for (let j = i + 1; j < cost.length; j++) {
16+
if (budget - cost[j] == 0) {
17+
console.log(`${i + 1} ${j + 1}`);
18+
return [i + 1, j + 1];
19+
}
20+
}
21+
}
22+
23+
return [];
24+
}
25+
26+
export function whatFlavors(cost: number[], money: number): void {
27+
console.log(what_flavors_bruteforce_compute(cost, money)?.join(' '));
28+
}
29+
30+
export default { what_flavors_bruteforce_compute, whatFlavors };

0 commit comments

Comments
 (0)