Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def combine(self) -> bool:
- """Turn this Block into a leaf based on the majority colour of its
- children.
- The majority colour is the colour with the most child blocks of that
- colour. A tie does not constitute a majority (e.g., if there are two red
- children and two blue children, then there is no majority colour).
- If there is no majority colour, do nothing. If this block is not at a
- level of max_depth - 1, or this block has no children, do nothing.
- Return True iff this Block was turned into a leaf node.
- """
- # TODO: Implement me
- if self.level != self.max_depth - 1 or len(self.children) == 0:
- return False
- else:
- d = {}
- for c in self.children:
- if c.colour not in d:
- d[c.colour] = 1
- else:
- d[c.colour] += 1
- m = 0
- clr = None
- for i in d:
- if d[i] > m:
- m = d[i]
- clr = i
- # checking for duplicates
- for i in d:
- if d[i] == m and clr != i:
- return False
- return True
- def create_copy(self) -> Block:
- """Return a new Block that is a deep copy of this Block.
- Remember that a deep copy has new blocks (not aliases) at every level.
- """
- # TODO: Implement me
- copy = Block(self.position, self.size, self.colour, self.level,
- self.max_depth)
- for child in self.children:
- copy.children.append(child.create_copy())
- return copy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement