-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0da8ce0
Showing
11 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
venv | ||
.idea | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from typing import Dict, List | ||
|
||
|
||
class ScoreCalculator: | ||
def __init__(self, pizzas: Dict[int, List[int]]): | ||
self.pizzas = pizzas | ||
|
||
def calculate(self, entries: List[List[int]]) -> int: | ||
if len(entries) == 0: | ||
result = 0 | ||
else: | ||
result = sum(map(self.calculate_entry, entries)) | ||
return result | ||
|
||
def calculate_entry(self, entry: List[int]) -> int: | ||
ingredients = [] | ||
for pizza in entry[1:]: | ||
ingredients.extend(self.pizzas.get(pizza)) | ||
return len(set(ingredients))**2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2 | ||
2 1 4 | ||
3 0 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1 | ||
2 1 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1 | ||
3 0 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1 | ||
4 0 1 2 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2 | ||
3 4 3 2 | ||
2 1 0 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
|
||
from score_calculator import ScoreCalculator | ||
|
||
|
||
class TestScoreCalculator(): | ||
@pytest.fixture | ||
def calculator(self): | ||
pizzas = {0: [0, 1, 2], | ||
1: [3, 4, 5], | ||
2: [6, 3, 1], | ||
3: [4, 3, 5], | ||
4: [6, 5]} | ||
calculator = ScoreCalculator(pizzas) | ||
yield calculator | ||
|
||
def test_calculate_scores(self, calculator): | ||
solution_a = [[2, 1, 4], [3, 0, 2, 3]] | ||
solution_b = [] | ||
solution_c = [[2, 1, 4]] | ||
solution_d = [[4, 0, 1, 2, 4]] | ||
solution_e = [[3, 4, 3, 2], [2, 1, 0]] | ||
solution_f = [[3, 0, 2, 3]] | ||
|
||
assert calculator.calculate(solution_a) == 65 | ||
assert calculator.calculate(solution_b) == 0 | ||
assert calculator.calculate(solution_c) == 16 | ||
assert calculator.calculate(solution_d) == 49 | ||
assert calculator.calculate(solution_e) == 61 | ||
assert calculator.calculate(solution_f) == 49 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
|
||
from validator import Validator | ||
|
||
|
||
class TestValidator(): | ||
@pytest.fixture | ||
def validator(self): | ||
num_pizzas = 30 | ||
teams_of_2 = 1 | ||
teams_of_3 = 2 | ||
teams_of_4 = 1 | ||
validator = Validator(teams_of_2, teams_of_3, teams_of_4, num_pizzas) | ||
yield validator | ||
|
||
def test_valid_solutions(self, validator): | ||
solution_a = [[2, 1, 4], [3, 0, 2, 3]] | ||
solution_b = [] | ||
solution_c = [[2, 1, 4]] | ||
solution_d = [[4, 0, 1, 2, 4]] | ||
solution_e = [[3, 4, 3, 2], [2, 1, 0]] | ||
|
||
assert validator.validate(solution_a) | ||
assert validator.validate(solution_b) | ||
assert validator.validate(solution_c) | ||
assert validator.validate(solution_d) | ||
assert validator.validate(solution_e) | ||
|
||
def test_invalid_solutions(self, validator): | ||
solution_a = [[2, 1], [3, 0, 2, 3]] # less pizzas than people | ||
solution_b = [[2, 0, 1], [3, 2, 3, 4], [3, 5, 6, 7], [4, 8, 9, 10, 11], | ||
[2, 12, 13]] # more deliveries than teams | ||
solution_c = [[]] # invalid format | ||
solution_d = [[2, 10, 500]] # invalid pizza id | ||
solution_e = [[2, 1, 2], [2, 0, 1]] # duplicated pizza id across two teams | ||
solution_f = [[2, 1, 1], [2, 0, 2]] # duplicated pizza id in same team | ||
|
||
assert not validator.validate(solution_a) | ||
assert not validator.validate(solution_b) | ||
assert not validator.validate(solution_c) | ||
assert not validator.validate(solution_d) | ||
assert not validator.validate(solution_e) | ||
assert not validator.validate(solution_f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import List | ||
|
||
|
||
class Validator: | ||
def __init__(self, teams_of_2: int, teams_of_3: int, teams_of_4: int, num_pizzas: int): | ||
self.teams_of_2 = teams_of_2 | ||
self.teams_of_3 = teams_of_3 | ||
self.teams_of_4 = teams_of_4 | ||
self.num_teams = teams_of_2 + teams_of_3 + teams_of_4 | ||
self.num_pizzas = num_pizzas | ||
|
||
def validate(self, entries: List[List[int]]) -> bool: | ||
if len(entries) == 0: | ||
result = True | ||
else: | ||
result = len(entries) <= self.num_teams and \ | ||
all(self.validate_entry(entry) for entry in entries) | ||
if result: | ||
pizza_ids = [pizza_id for entry in entries for pizza_id in entry[1:]] | ||
result = result and len(pizza_ids) == len(set(pizza_ids)) | ||
return result | ||
|
||
def validate_entry(self, entry: List[int]) -> bool: | ||
if len(entry) == 0: | ||
result = False | ||
else: | ||
pizzas = entry[1:] | ||
valid_numbers = len(set(pizzas)) == entry[0] # number of people not equal to pizzas | ||
valid_ids = all(i < self.num_pizzas for i in pizzas) | ||
result = valid_numbers and valid_ids | ||
return result |