Guest User

Untitled

a guest
Jul 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. def get_pupil_from_direction(classroom, x, y, dx, dy):
  2. x += dx
  3. y += dy
  4. if min(x, y) < 0:
  5. return None
  6. if y >= len(classroom):
  7. return None
  8. if x >= len(classroom[y]):
  9. return None
  10. return classroom[y][x]
  11.  
  12.  
  13. def get_near_pupils(classroom, x, y):
  14. directions = [
  15. (-1, -1),
  16. (-1, 0),
  17. (-1, 1),
  18. (0, -1),
  19. (0, 1),
  20. (1, -1),
  21. (1, 0),
  22. (1, 1),
  23. ]
  24. pupils = []
  25. for dx, dy in directions:
  26. pupil = get_pupil_from_direction(classroom, x, y, dx, dy)
  27. if pupil is not None:
  28. pupils.append(pupil)
  29. return pupils
  30.  
  31.  
  32. def get_next_to_pupils(classroom, x, y):
  33. directions = [
  34. (-1, 0),
  35. (0, -1),
  36. (0, 1),
  37. (1, 0),
  38. ]
  39. pupils = []
  40. for dx, dy in directions:
  41. pupil = get_pupil_from_direction(classroom, x, y, dx, dy)
  42. if pupil is not None:
  43. pupils.append(pupil)
  44. return pupils
  45.  
  46. def get_other_pupil(promise, this_pupil):
  47. for pupil in promise:
  48. if pupil != this_pupil:
  49. return pupil
  50.  
  51. def evaluate(*, classroom, front, back,
  52. next_to, near_to, not_next_to, not_near):
  53. num_broken_promises = 0
  54.  
  55. # 1. Count front row broken promises
  56. front_row = classroom[0]
  57. for pupil in front:
  58. if pupil not in front_row:
  59. print(pupil + ' not in front row')
  60. num_broken_promises += 1
  61.  
  62. # TODO: 2. Count back row broken promises
  63.  
  64. for x, row in enumerate(classroom):
  65. for y, pupil in enumerate(row):
  66. near_pupils = get_near_pupils(classroom, x, y)
  67. next_pupils = get_next_to_pupils(classroom, x, y)
  68.  
  69. # 3. Count next_to broken promises
  70. related_next_to_promises = [promise for promise in next_to if pupil in promise]
  71. for promise in related_next_to_promises:
  72. other_pupil = get_other_pupil(promise, pupil)
  73. if other_pupil not in next_pupils:
  74. print(pupil + ' not next to ' + other_pupil)
  75. num_broken_promises += 1
  76.  
  77. # TODO: 4. Count near to broken promises
  78.  
  79. # TODO: 5. Count not next to broken promises
  80.  
  81. # TODO: 6. Count not near broken promises
  82.  
  83. return num_broken_promises
  84.  
  85.  
  86. score = evaluate(
  87. classroom=[
  88. ('tomas', 'olav', 'fredrik'),
  89. ('øystein', 'odd', 'martin'),
  90. ],
  91. front=['olav', 'odd'],
  92. back=['øystein'],
  93. next_to=[
  94. ('tomas', 'olav'),
  95. ('odd', 'martin')
  96. ],
  97. near_to=[
  98. ('tomas', 'odd')
  99. ],
  100. not_near=[
  101. ('fredrik', 'øystein'),
  102. ('fredrik', 'tomas'),
  103. ],
  104. not_next_to=[
  105. ('olav', 'fredrik')
  106. ],
  107. )
Add Comment
Please, Sign In to add comment