Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. def combine(self) -> bool:
  2. """Turn this Block into a leaf based on the majority colour of its
  3. children.
  4.  
  5. The majority colour is the colour with the most child blocks of that
  6. colour. A tie does not constitute a majority (e.g., if there are two red
  7. children and two blue children, then there is no majority colour).
  8.  
  9. If there is no majority colour, do nothing. If this block is not at a
  10. level of max_depth - 1, or this block has no children, do nothing.
  11.  
  12. Return True iff this Block was turned into a leaf node.
  13. """
  14. # TODO: Implement me
  15. if self.level != self.max_depth - 1 or len(self.children) == 0:
  16. return False
  17. else:
  18. d = {}
  19.  
  20. for c in self.children:
  21. if c.colour not in d:
  22. d[c.colour] = 1
  23. else:
  24. d[c.colour] += 1
  25.  
  26. m = 0
  27. clr = None
  28. for i in d:
  29. if d[i] > m:
  30. m = d[i]
  31. clr = i
  32.  
  33. # checking for duplicates
  34. for i in d:
  35. if d[i] == m and clr != i:
  36. return False
  37. return True
  38.  
  39. def create_copy(self) -> Block:
  40. """Return a new Block that is a deep copy of this Block.
  41.  
  42. Remember that a deep copy has new blocks (not aliases) at every level.
  43. """
  44. # TODO: Implement me
  45. copy = Block(self.position, self.size, self.colour, self.level,
  46. self.max_depth)
  47.  
  48. for child in self.children:
  49. copy.children.append(child.create_copy())
  50.  
  51. return copy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement