Skip to content

Commit d8f3459

Browse files
authored
Merge pull request #562 from sir-gon/feature/ctci-ice-cream-parlor
Feature/ctci ice cream parlor
2 parents 34c1893 + 8631345 commit d8f3459

File tree

6 files changed

+120
-64
lines changed

6 files changed

+120
-64
lines changed

docs/hackerrank/interview_preparation_kit/search/ctci-ice-cream-parlor.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# [Hash Tables: Ice Cream Parlor](https://www.hackerrank.com/challenges/ctci-ice-cream-parlor)
1+
# [Search: Hash Tables: Ice Cream Parlor](https://www.hackerrank.com/challenges/ctci-ice-cream-parlor)
22

33
- Difficulty: `#medium`
4-
- Category: `#ProblemSolvingIntermediate`
4+
- Category: `#ProblemSolvingIntermediate` `#Hashtables` `#Search`
55

66
Each time Sunny and Johnny take a trip to the Ice Cream Parlor,
77
they pool their money to buy ice cream.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"tests": [
5+
{
6+
"costs": [1, 4, 5, 3, 2],
7+
"money": 90,
8+
"expected": null
9+
}
10+
]
11+
}
12+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"tests": [
5+
{
6+
"costs": [1, 4, 5, 3, 2],
7+
"money": 4,
8+
"expected": [1, 4]
9+
},
10+
{
11+
"costs": [2, 2, 4, 3],
12+
"money": 4,
13+
"expected": [1, 2]
14+
}
15+
]
16+
},
17+
{
18+
"title": "Sample Test Case 1",
19+
"tests": [
20+
{
21+
"costs": [1, 2, 3, 5, 6],
22+
"money": 5,
23+
"expected": [2, 3]
24+
}
25+
]
26+
},
27+
{
28+
"title": "Sample Test Case 2",
29+
"tests": [
30+
{
31+
"costs": [4, 3, 2, 5, 7],
32+
"money": 8,
33+
"expected": [2, 4]
34+
},
35+
{
36+
"costs": [7, 2, 5, 4, 11],
37+
"money": 12,
38+
"expected": [1, 3]
39+
}
40+
]
41+
}
42+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def what_flavors_brute_force(cost: list[int], money: int) -> list[int] | None:
2+
3+
for i, x in enumerate(cost):
4+
5+
budget = money - x
6+
7+
for j in range(i + 1, len(cost)):
8+
if budget - cost[j] == 0:
9+
print(f'{i + 1} {j + 1}')
10+
return [i + 1, j + 1]
11+
12+
return None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unittest
2+
import os
3+
from pathlib import Path
4+
5+
from ....hackerrank.lib.loader import load_test_cases
6+
from .ctci_ice_cream_parlor_bruteforce import what_flavors_brute_force
7+
8+
BRUTEFORCE = os.getenv('BRUTEFORCE')
9+
BRUTEFORCE = BRUTEFORCE.upper() == "TRUE" if BRUTEFORCE is not None else False
10+
11+
FILE_PATH = str(Path(__file__).resolve().parent)
12+
13+
TEST_CASES_SMALL_CASES = load_test_cases(
14+
FILE_PATH + '/ctci_ice_cream_parlor.testcases.json')
15+
TEST_CASES_BORDER_CASES = load_test_cases(
16+
FILE_PATH + '/ctci_ice_cream_parlor.border_testcases.json')
17+
18+
19+
class TestIceCreamParlorBruteForce(unittest.TestCase):
20+
21+
# @unittest.skipIf(not BRUTEFORCE, "skipping due a is a large BRUTEFORCE test")
22+
def test_what_flavors_brute_force(self):
23+
24+
for _, testset in enumerate(TEST_CASES_SMALL_CASES):
25+
26+
for _, _tt in enumerate(testset['tests']):
27+
28+
self.assertEqual(
29+
what_flavors_brute_force(_tt['costs'], _tt['money']), _tt['expected'],
30+
f"{_} | what_flavors_brute_force({_tt['costs']}, {_tt['money']}) "
31+
f"=> must be {_tt['expected']}")
32+
33+
def test_what_flavors_brute_force_border_case(self):
34+
35+
for _, testset in enumerate(TEST_CASES_BORDER_CASES):
36+
37+
for _, _tt in enumerate(testset['tests']):
38+
39+
self.assertEqual(
40+
what_flavors_brute_force(_tt['costs'], _tt['money']), _tt['expected'],
41+
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
42+
f"=> {_tt['expected']}")
Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,15 @@
11
import unittest
2+
from pathlib import Path
23

4+
from ....hackerrank.lib.loader import load_test_cases
35
from .ctci_ice_cream_parlor import what_flavors
46

57

6-
TEST_CASES = [
7-
{
8-
'title': 'Sample Test Case 0',
9-
'tests':
10-
[
11-
{
12-
'costs': [1, 4, 5, 3, 2],
13-
'money': 4,
14-
'answer': [1, 4]
15-
},
16-
{
17-
'costs': [2, 2, 4, 3],
18-
'money': 4,
19-
'answer': [1, 2]
20-
}
21-
]
22-
},
23-
{
24-
'title': 'Sample Test Case 1',
25-
'tests':
26-
[
27-
{
28-
'costs': [1, 2, 3, 5, 6],
29-
'money': 5,
30-
'answer': [2, 3]
31-
}
32-
]
33-
},
34-
{
35-
'title': 'Sample Test Case 2',
36-
'tests':
37-
[
38-
{
39-
'costs': [4, 3, 2, 5, 7],
40-
'money': 8,
41-
'answer': [2, 4]
42-
},
43-
{
44-
'costs': [7, 2, 5, 4, 11],
45-
'money': 12,
46-
'answer': [1, 3]
47-
}
48-
]
49-
}
50-
]
8+
FILE_PATH = str(Path(__file__).resolve().parent)
519

52-
TEST_CASES_BORDER_CASES = [
53-
{
54-
'title': 'Sample Test Case 0',
55-
'tests':
56-
[
57-
{
58-
'costs': [1, 4, 5, 3, 2],
59-
'money': 90,
60-
'answer': None
61-
}
62-
]
63-
}
64-
]
10+
TEST_CASES = load_test_cases(FILE_PATH + '/ctci_ice_cream_parlor.testcases.json')
11+
TEST_CASES_BORDER_CASES = load_test_cases(
12+
FILE_PATH + '/ctci_ice_cream_parlor.border_testcases.json')
6513

6614

6715
class TestIceCreamParlor(unittest.TestCase):
@@ -73,9 +21,9 @@ def test_what_flavors(self):
7321
for _, _tt in enumerate(testset['tests']):
7422

7523
self.assertEqual(
76-
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
24+
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
7725
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
78-
f"=> {_tt['answer']}")
26+
f"=> {_tt['expected']}")
7927

8028
def test_what_flavors_border_case(self):
8129

@@ -84,6 +32,6 @@ def test_what_flavors_border_case(self):
8432
for _, _tt in enumerate(testset['tests']):
8533

8634
self.assertEqual(
87-
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
35+
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
8836
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
89-
f"=> {_tt['answer']}")
37+
f"=> {_tt['expected']}")

0 commit comments

Comments
 (0)