Skip to content

Feature/ctci ice cream parlor #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# [Hash Tables: Ice Cream Parlor](https://www.hackerrank.com/challenges/ctci-ice-cream-parlor)
# [Search: Hash Tables: Ice Cream Parlor](https://www.hackerrank.com/challenges/ctci-ice-cream-parlor)

- Difficulty: `#medium`
- Category: `#ProblemSolvingIntermediate`
- Category: `#ProblemSolvingIntermediate` `#Hashtables` `#Search`

Each time Sunny and Johnny take a trip to the Ice Cream Parlor,
they pool their money to buy ice cream.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"title": "Sample Test Case 0",
"tests": [
{
"costs": [1, 4, 5, 3, 2],
"money": 90,
"expected": null
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"title": "Sample Test Case 0",
"tests": [
{
"costs": [1, 4, 5, 3, 2],
"money": 4,
"expected": [1, 4]
},
{
"costs": [2, 2, 4, 3],
"money": 4,
"expected": [1, 2]
}
]
},
{
"title": "Sample Test Case 1",
"tests": [
{
"costs": [1, 2, 3, 5, 6],
"money": 5,
"expected": [2, 3]
}
]
},
{
"title": "Sample Test Case 2",
"tests": [
{
"costs": [4, 3, 2, 5, 7],
"money": 8,
"expected": [2, 4]
},
{
"costs": [7, 2, 5, 4, 11],
"money": 12,
"expected": [1, 3]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def what_flavors_brute_force(cost: list[int], money: int) -> list[int] | None:

for i, x in enumerate(cost):

Check notice on line 3 in src/hackerrank/interview_preparation_kit/search/ctci_ice_cream_parlor_bruteforce.py

View check run for this annotation

codefactor.io / CodeFactor

src/hackerrank/interview_preparation_kit/search/ctci_ice_cream_parlor_bruteforce.py#L3

Variable name "x" doesn't conform to snake_case naming style (invalid-name)

budget = money - x

for j in range(i + 1, len(cost)):
if budget - cost[j] == 0:
print(f'{i + 1} {j + 1}')
return [i + 1, j + 1]

return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
import os
from pathlib import Path

from ....hackerrank.lib.loader import load_test_cases
from .ctci_ice_cream_parlor_bruteforce import what_flavors_brute_force

BRUTEFORCE = os.getenv('BRUTEFORCE')
BRUTEFORCE = BRUTEFORCE.upper() == "TRUE" if BRUTEFORCE is not None else False

FILE_PATH = str(Path(__file__).resolve().parent)

TEST_CASES_SMALL_CASES = load_test_cases(
FILE_PATH + '/ctci_ice_cream_parlor.testcases.json')
TEST_CASES_BORDER_CASES = load_test_cases(
FILE_PATH + '/ctci_ice_cream_parlor.border_testcases.json')


class TestIceCreamParlorBruteForce(unittest.TestCase):

# @unittest.skipIf(not BRUTEFORCE, "skipping due a is a large BRUTEFORCE test")
def test_what_flavors_brute_force(self):

for _, testset in enumerate(TEST_CASES_SMALL_CASES):

for _, _tt in enumerate(testset['tests']):

self.assertEqual(
what_flavors_brute_force(_tt['costs'], _tt['money']), _tt['expected'],
f"{_} | what_flavors_brute_force({_tt['costs']}, {_tt['money']}) "
f"=> must be {_tt['expected']}")

def test_what_flavors_brute_force_border_case(self):

for _, testset in enumerate(TEST_CASES_BORDER_CASES):

for _, _tt in enumerate(testset['tests']):

self.assertEqual(
what_flavors_brute_force(_tt['costs'], _tt['money']), _tt['expected'],
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
f"=> {_tt['expected']}")
Original file line number Diff line number Diff line change
@@ -1,67 +1,15 @@
import unittest
from pathlib import Path

from ....hackerrank.lib.loader import load_test_cases
from .ctci_ice_cream_parlor import what_flavors


TEST_CASES = [
{
'title': 'Sample Test Case 0',
'tests':
[
{
'costs': [1, 4, 5, 3, 2],
'money': 4,
'answer': [1, 4]
},
{
'costs': [2, 2, 4, 3],
'money': 4,
'answer': [1, 2]
}
]
},
{
'title': 'Sample Test Case 1',
'tests':
[
{
'costs': [1, 2, 3, 5, 6],
'money': 5,
'answer': [2, 3]
}
]
},
{
'title': 'Sample Test Case 2',
'tests':
[
{
'costs': [4, 3, 2, 5, 7],
'money': 8,
'answer': [2, 4]
},
{
'costs': [7, 2, 5, 4, 11],
'money': 12,
'answer': [1, 3]
}
]
}
]
FILE_PATH = str(Path(__file__).resolve().parent)

TEST_CASES_BORDER_CASES = [
{
'title': 'Sample Test Case 0',
'tests':
[
{
'costs': [1, 4, 5, 3, 2],
'money': 90,
'answer': None
}
]
}
]
TEST_CASES = load_test_cases(FILE_PATH + '/ctci_ice_cream_parlor.testcases.json')
TEST_CASES_BORDER_CASES = load_test_cases(
FILE_PATH + '/ctci_ice_cream_parlor.border_testcases.json')


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

self.assertEqual(
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
f"=> {_tt['answer']}")
f"=> {_tt['expected']}")

def test_what_flavors_border_case(self):

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

self.assertEqual(
what_flavors(_tt['costs'], _tt['money']), _tt['answer'],
what_flavors(_tt['costs'], _tt['money']), _tt['expected'],
f"{_} | what_flavors({_tt['costs']}, {_tt['money']}) must be "
f"=> {_tt['answer']}")
f"=> {_tt['expected']}")