Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_pupil_from_direction(classroom, x, y, dx, dy):
- x += dx
- y += dy
- if min(x, y) < 0:
- return None
- if y >= len(classroom):
- return None
- if x >= len(classroom[y]):
- return None
- return classroom[y][x]
- def get_near_pupils(classroom, x, y):
- directions = [
- (-1, -1),
- (-1, 0),
- (-1, 1),
- (0, -1),
- (0, 1),
- (1, -1),
- (1, 0),
- (1, 1),
- ]
- pupils = []
- for dx, dy in directions:
- pupil = get_pupil_from_direction(classroom, x, y, dx, dy)
- if pupil is not None:
- pupils.append(pupil)
- return pupils
- def get_next_to_pupils(classroom, x, y):
- directions = [
- (-1, 0),
- (0, -1),
- (0, 1),
- (1, 0),
- ]
- pupils = []
- for dx, dy in directions:
- pupil = get_pupil_from_direction(classroom, x, y, dx, dy)
- if pupil is not None:
- pupils.append(pupil)
- return pupils
- def get_other_pupil(promise, this_pupil):
- for pupil in promise:
- if pupil != this_pupil:
- return pupil
- def evaluate(*, classroom, front, back,
- next_to, near_to, not_next_to, not_near):
- num_broken_promises = 0
- # 1. Count front row broken promises
- front_row = classroom[0]
- for pupil in front:
- if pupil not in front_row:
- print(pupil + ' not in front row')
- num_broken_promises += 1
- # TODO: 2. Count back row broken promises
- for x, row in enumerate(classroom):
- for y, pupil in enumerate(row):
- near_pupils = get_near_pupils(classroom, x, y)
- next_pupils = get_next_to_pupils(classroom, x, y)
- # 3. Count next_to broken promises
- related_next_to_promises = [promise for promise in next_to if pupil in promise]
- for promise in related_next_to_promises:
- other_pupil = get_other_pupil(promise, pupil)
- if other_pupil not in next_pupils:
- print(pupil + ' not next to ' + other_pupil)
- num_broken_promises += 1
- # TODO: 4. Count near to broken promises
- # TODO: 5. Count not next to broken promises
- # TODO: 6. Count not near broken promises
- return num_broken_promises
- score = evaluate(
- classroom=[
- ('tomas', 'olav', 'fredrik'),
- ('øystein', 'odd', 'martin'),
- ],
- front=['olav', 'odd'],
- back=['øystein'],
- next_to=[
- ('tomas', 'olav'),
- ('odd', 'martin')
- ],
- near_to=[
- ('tomas', 'odd')
- ],
- not_near=[
- ('fredrik', 'øystein'),
- ('fredrik', 'tomas'),
- ],
- not_next_to=[
- ('olav', 'fredrik')
- ],
- )
Add Comment
Please, Sign In to add comment