Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos3dx committed Jan 28, 2021
0 parents commit 0da8ce0
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
venv
.idea
.DS_Store
19 changes: 19 additions & 0 deletions score_calculator.py
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
3 changes: 3 additions & 0 deletions test_submitions/sub_a
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
2 1 4
3 0 2 3
2 changes: 2 additions & 0 deletions test_submitions/sub_b
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
2 1 4
2 changes: 2 additions & 0 deletions test_submitions/sub_c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
3 0 2 3
2 changes: 2 additions & 0 deletions test_submitions/sub_d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
4 0 1 2 4
3 changes: 3 additions & 0 deletions test_submitions/sub_e
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
3 4 3 2
2 1 0
Empty file added unit_tests/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions unit_tests/test_score_calculator.py
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

43 changes: 43 additions & 0 deletions unit_tests/test_validator.py
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)
31 changes: 31 additions & 0 deletions validator.py
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

0 comments on commit 0da8ce0

Please sign in to comment.