Skip to content

Commit

Permalink
Solve Ice Cream Parlor
Browse files Browse the repository at this point in the history
  • Loading branch information
gusaiani committed Mar 22, 2020
1 parent 9f0c902 commit d066ed1
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .eslintrc → .eslintrc.json
@@ -1,6 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 2019,
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
Expand All @@ -17,4 +17,3 @@
"jest/globals": true
}
}

3 changes: 1 addition & 2 deletions .prettierrc → .prettierrc.json
Expand Up @@ -12,7 +12,6 @@
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"trailingComma": "es5",
"useTabs": false
}

4 changes: 2 additions & 2 deletions appleAndOrange.js
Expand Up @@ -7,14 +7,14 @@ function countApplesAndOranges(
appleTree,
orangeTree,
apples,
oranges,
oranges
) {
const applesOnHouse = fruitsOnHouse(houseStart, houseEnd, appleTree, apples)
const orangesOnHouse = fruitsOnHouse(
houseStart,
houseEnd,
orangeTree,
oranges,
oranges
)
return [applesOnHouse, orangesOnHouse]
}
Expand Down
2 changes: 1 addition & 1 deletion beautifulDays.js
Expand Up @@ -14,7 +14,7 @@ function inverse(num) {
(num + '')
.split('')
.reverse()
.join(''),
.join('')
)
}

Expand Down
2 changes: 1 addition & 1 deletion cutTheStick.js
Expand Up @@ -5,7 +5,7 @@ function cutTheStick(arr) {
arr[item] = arr[item] ? arr[item] + 1 : 1
return {arr, count: count + 1}
},
{arr: new Array(), count: 0},
{arr: new Array(), count: 0}
)

const tally = reduction.arr
Expand Down
39 changes: 39 additions & 0 deletions iceCreamParlor.js
@@ -0,0 +1,39 @@
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem
function iceCreamParlor(cost, money) {
const hashTable = {}

for (let i = 0; i < cost.length; i++) {
if (cost[i] < money) {
if (hashTable[money - cost[i]] !== undefined) {
return [hashTable[money - cost[i]] + 1, i + 1]
}
hashTable[cost[i]] = i
}
}
}

/*
* Slow implementation
*
function iceCreamParlor(cost, money) {
const hashTable = {}
for (let i = 0; i < cost.length; i++) {
if (cost[i] < money) {
hashTable[i] = cost[i]
}
}
const keys = Object.keys(hashTable)
for(let i = 0; i < keys.length - 1; i++) {
for (let j = i + 1; j < keys.length; j++) {
if (hashTable[keys[i]] + hashTable[keys[j]] === money) {
return [parseInt(keys[i])+1, parseInt(keys[j])+1]
}
}
}
}
*/

module.exports = iceCreamParlor
2 changes: 1 addition & 1 deletion question1.js
Expand Up @@ -20,7 +20,7 @@ function getChange(money, price) {

return acc
},
{change, changeCoins: []},
{change, changeCoins: []}
)

return changeCoins.reverse()
Expand Down
2 changes: 1 addition & 1 deletion repeatedStrings.js
Expand Up @@ -19,7 +19,7 @@ function repeatedStrings(string, chars) {
}
return [inRem, inStrLen]
},
[0, 0],
[0, 0]
)

return div * lettersToFindInStringLength + lettersToFindInRem
Expand Down
5 changes: 4 additions & 1 deletion saveThePrisoner.js
@@ -1,8 +1,11 @@
// https://www.hackerrank.com/challenges/save-the-prisoner/problem
function saveThePrisoner(prisoners, candies, startAt) {
const a = startAt + candies - 1

if (a > prisoners) {
if (a % prisoners == 0) return prisoners
if (a % prisoners == 0) {
return prisoners
}
return a % prisoners
}
return a
Expand Down
4 changes: 2 additions & 2 deletions tests/closestNumbers.test.js
Expand Up @@ -5,14 +5,14 @@ const closestNumbers = require('../closestNumbers')

test('A closestNumbers test', () => {
const numbers = '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854'.split(
' ',
' '
)
expect(closestNumbers(numbers)).toEqual('-20 30')
})

test('A closestNumbers test', () => {
const numbers = '-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854 -520 -470'.split(
' ',
' '
)
expect(closestNumbers(numbers)).toEqual('-520 -470 -20 30')
})
Expand Down
2 changes: 1 addition & 1 deletion tests/flattenArbitrarilyDeepArray.test.js
Expand Up @@ -10,6 +10,6 @@ test('Flatten an array of depth 3', () => {

test('Flatten an array of depth 5', () => {
expect(
flattenArbitrarilyDeepArray([[1, 2, [3]], 4, [5, [6, [7, 8]]]]),
flattenArbitrarilyDeepArray([[1, 2, [3]], 4, [5, [6, [7, 8]]]])
).toEqual([1, 2, 3, 4, 5, 6, 7, 8])
})
16 changes: 16 additions & 0 deletions tests/iceCreamParlor.test.js
@@ -0,0 +1,16 @@
const iceCreamParlor = require('../iceCreamParlor')

test('An iceCreamParlor test', () => {
const cost = [2, 1, 3, 5, 6]
const money = 5
const expectedResult = [1, 3]

expect(iceCreamParlor(cost, money)).toEqual(expectedResult)
})

test('Another iceCreamParlor test', () => {
const cost = [1, 4, 5, 3, 2]
const money = 4
const expectedResult = [1, 4]
expect(iceCreamParlor(cost, money)).toEqual(expectedResult)
})
2 changes: 1 addition & 1 deletion tests/saveThePrisoner.test.js
Expand Up @@ -225,7 +225,7 @@ examplesArray.forEach((line, index) => {

test(`A saveThePrisoner test with ${prisoners}, ${candies} and ${startAt} which should return ${answersArray[index]}`, () => {
expect(saveThePrisoner(prisoners, candies, startAt)).toEqual(
parseInt(answersArray[index]),
parseInt(answersArray[index])
)
})
})
2 changes: 1 addition & 1 deletion tests/staircase.test.js
Expand Up @@ -6,6 +6,6 @@ const staircase = require('../staircase')

test('A staircase of 6', () => {
expect(staircase(6)).toEqual(
' #\n ##\n ###\n ####\n #####\n######\n',
' #\n ##\n ###\n ####\n #####\n######\n'
)
})
2 changes: 1 addition & 1 deletion twoPluses.js
Expand Up @@ -3,7 +3,7 @@ function twoPluses(grid) {
const hash = buildHash(grid)
const pluses = compilePluses(hash)
const largestPlusesNotTouchingEachOther = findLargestPlusesNotTouchingOneAnother(
pluses,
pluses
)

if (!largestPlusesNotTouchingEachOther) return 0
Expand Down

0 comments on commit d066ed1

Please sign in to comment.