Skip to content

Commit f8bc39d

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Miscellaneous: Ice Cream Parlor. Brute force solution added.
1 parent 5f2374b commit f8bc39d

File tree

5 files changed

+127
-62
lines changed

5 files changed

+127
-62
lines changed
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 []
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
import json
3+
from pathlib import Path
4+
import os
5+
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+
12+
FILE_PATH = str(Path(__file__).resolve().parent)
13+
14+
with open(
15+
FILE_PATH +
16+
'/ctci_ice_cream_parlor.testcases.json',
17+
encoding="utf-8"
18+
) as file1:
19+
TEST_CASES = json.load(file1)
20+
21+
with open(
22+
FILE_PATH +
23+
'/ctci_ice_cream_parlor.border_testcases.json',
24+
encoding="utf-8"
25+
) as file2:
26+
TEST_CASES_BORDER_CASES = json.load(file2)
27+
28+
29+
class TestIceCreamParlor(unittest.TestCase):
30+
31+
@unittest.skipIf(not BRUTEFORCE, "skipping due a is a large BRUTEFORCE test")
32+
def test_what_flavors_brute_force(self):
33+
34+
for _, testset in enumerate(TEST_CASES):
35+
36+
for _, _tt in enumerate(testset['tests']):
37+
38+
self.assertEqual(
39+
what_flavors_brute_force(_tt['costs'], _tt['money']), _tt['expected'],
40+
f"{_} | what_flavors_brute_force({_tt['costs']}, {_tt['money']}) "
41+
f"=> must be {_tt['expected']}")
Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,25 @@
11
import unittest
2+
import json
3+
from pathlib import Path
24

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+
with open(
11+
FILE_PATH +
12+
'/ctci_ice_cream_parlor.testcases.json',
13+
encoding="utf-8"
14+
) as file1:
15+
TEST_CASES = json.load(file1)
16+
17+
with open(
18+
FILE_PATH +
19+
'/ctci_ice_cream_parlor.border_testcases.json',
20+
encoding="utf-8"
21+
) as file2:
22+
TEST_CASES_BORDER_CASES = json.load(file2)
6523

6624

6725
class TestIceCreamParlor(unittest.TestCase):
@@ -73,9 +31,9 @@ def test_what_flavors(self):
7331
for _, _tt in enumerate(testset['tests']):
7432

7533
self.assertEqual(
76-
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
34+
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
7735
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
78-
f"=> {_tt['answer']}")
36+
f"=> {_tt['expected']}")
7937

8038
def test_what_flavors_border_case(self):
8139

@@ -84,6 +42,6 @@ def test_what_flavors_border_case(self):
8442
for _, _tt in enumerate(testset['tests']):
8543

8644
self.assertEqual(
87-
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
45+
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
8846
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
89-
f"=> {_tt['answer']}")
47+
f"=> {_tt['expected']}")

0 commit comments

Comments
 (0)