Embercode
✕
Segment TreesQuick quiz

Lazy assignments

7 questions. Covers Range assign and range add with lazy tags in Python.

Questions 1 to 5 of 7

  1. 1. What does this program print, one line per print? ``python from dataclasses import dataclass @dataclass(frozen=True) class Tag: is_set: bool value: int add: int def then(self, later): if later.is_set: return later return Tag(self.is_set, self.value, self.add + later.add) NONE = Tag(False, 0, 0) print(Tag(False, 0, 9).then(Tag(True, 5, 0))) print(Tag(True, 5, 0).then(Tag(False, 0, 2))) print(NONE.then(Tag(False, 0, 3))) ``
  2. 2. What does this program print, one value per line? ``python def apply_to_sum(old_sum, length, is_set, value, add): if is_set: return (value + add) * length return old_sum + add * length print(apply_to_sum(100, 3, True, 5, 2)) print(apply_to_sum(100, 3, False, 0, 4)) print(apply_to_sum(100, 4, True, 1, 0)) ``
  3. 3. A node has the pending tag "add 3", then receives "assign 4", then receives "add 1". Which single tag should it hold?
  4. 4. Why does push set the node's tag back to NONE after it passes the tag down to both children?
  5. 5. After assign 5 and then add 2 on a node, the node holds one combined tag. Write it as Python code.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.