15106 - RedCapeFlyingCat's Boxes   

Description

RedCapeFlyingCat is organizing its collection of numbered tokens into boxes. An ordinary box keeps the tokens in the order RedCapeFlyingCat puts them in. RedCapeFlyingCat also has a magic box that rearranges its tokens from smallest to largest every time something is put in.

RedCapeFlyingCat wants a program that keeps track of its boxes. Help it by implementing two classes, Box and SortedBox. A box stores a sequence of elements. Each element of a Box is either an integer or another box (a Box or a SortedBox); a SortedBox holds only integers.

Box

  • Box(L) creates a box holding the elements of the list L, in the same order.
  • X.add(v), where v is an integer or a box, appends v to the end of the box.
  • X.add(L), where L is a list, appends all elements of L to the end of the box, in order.
  • X.pop() removes the last element of the box. It prints nothing.
  • X.size is the number of elements in the box. A box inside it counts as one element, no matter what it holds. It is an attribute, not a method: the input always writes X.size, never X.size().
  • print(X) prints the elements separated by , (a comma and a space) inside square brackets, for example [3, 1, 2]. An empty box prints []. A box inside it is printed the way that box itself prints, for example [[1, 2], 3, (4, 5)].

A box put inside another box is that same box, not a copy: if it changes later, the outer box shows the change too. Assigning a new box to a variable does not change boxes that already hold the old one.

SortedBox

A SortedBox behaves exactly like a Box, except:

  • Its elements are always kept in non-decreasing order: right after it is created and after every add. Because of this, X.pop() on a SortedBox removes its largest element.
  • print(X) uses round parentheses instead of square brackets, for example (1, 2, 3). A SortedBox with one element prints like (5) (not (5,)), and an empty box prints ().

SortedBox is meant to be a subclass of Box: define it as class SortedBox(Box): and reuse what Box already does instead of writing everything again.

Default code

Start from the code below. Fill in the two classes, and keep the last three lines unchanged: they read the input and run each line as a Python statement.

class Box:
    pass


class SortedBox(Box):
    pass


t = int(input())
for _ in range(t):
    exec(input())

Input

The first line contains one integer , the number of statements.

Each of the next lines contains one Python statement in exactly one of the following forms, where X is a variable name, v is an integer or the variable name of a box, and L is a list of such values written like [3, -1, 2], [a, 6], or []:

  • X = Box(L)
  • X = SortedBox(L)
  • X.add(v)
  • X.add(L)
  • X.pop()
  • print(X)
  • print(X.size)

Constraints

  • Every integer is between and .
  • Each L has at most elements, and a box never holds more than elements.
  • Variable names are single lowercase letters other than t, and are assigned before use.
  • X.pop() is only called on a non-empty box.
  • A SortedBox only holds integers.
  • A box never contains itself, and at most boxes are nested inside one another.
  • Each print(X) prints at most integers and at most boxes, X included.

Output

Each print statement prints exactly one line. Output these lines in the order the statements are executed. The other statements print nothing.

  • print(X) on a Box prints the elements separated by , inside square brackets, such as [3, 1, 2] or []. A box inside it is printed in its own format, such as [[1, 2], 3, (4, 5)].
  • print(X) on a SortedBox prints the elements in non-decreasing order, separated by , , inside round parentheses, such as (1, 2, 3), (5), or ().
  • print(X.size) prints the number of elements in the box; a box inside it counts as one element.

Sample Input 1  Download

Sample Output 1  Download

Explanation 1

  • a = Box([3, 1, 2]): a holds .
  • a.add(5): a holds .
  • a.add([-4, 0]): a holds , so print(a) prints [3, 1, 2, 5, -4, 0].
  • a.pop(): the last element is removed, so print(a) prints [3, 1, 2, 5, -4] and print(a.size) prints 5.
  • b = SortedBox([3, 1, 2]): b holds .
  • b.add(-7): b holds .
  • b.add([10, 4]): b holds , so print(b) prints (-7, 1, 2, 3, 4, 10).
  • b.pop(): the largest element is removed, so print(b) prints (-7, 1, 2, 3, 4).
  • c = SortedBox([]): c is empty, so print(c) prints ().
  • c.add(8): c holds , so print(c) prints (8).

Sample Input 2  Download

Sample Output 2  Download

Explanation 2

  • a = Box([1, 2]) and b = SortedBox([5, 3]): a holds and b holds .
  • c = Box([a, 7, b]): c holds three elements: the box a, the integer , and the box b. Each inner box is printed in its own format, so print(c) prints [[1, 2], 7, (3, 5)], and print(c.size) prints 3.
  • a.add(9) and b.add(4): the boxes inside c are a and b themselves, not copies, so print(c) now prints [[1, 2, 9], 7, (3, 4, 5)].
  • d = Box([]) and c.add([d, 6]): the empty box d and the integer are appended, so print(c) prints [[1, 2, 9], 7, (3, 4, 5), [], 6] and print(c.size) prints 5.
  • a = Box([0]): the name a now refers to a new box, so print(a) prints [0]. The old box is still inside c, so print(c) prints the same as before.

Sample Input  Download

Sample Output  Download




Discuss