14815 - Math is not Fun   

Description

The head researcher, Professor Fish, loves efficiency.
However, he keeps changing his mind — sometimes he wants only odd numbers, sometimes squared ones.
You, the junior assistant, must make sure his number stream always comes out exactly how he wants.

Your task is to implement a simple math stream processor that applies a sequence of mathematical transformations to numbers.

A transformation is represented by a generator that takes numbers and yields processed results (for example, doubling, squaring, or filtering even numbers).

You will compose and process these transformations dynamically based on commands.

Help Professor Fish build and maintain a stream of transformations, and then apply them to an input sequence.

Available operations:

  • add transformation_name Add a new transformation to the end of the stream
  • remove transofrmation_name Remove the first occurrence of the transformation
  • process n Process the numbers from 1 to n (inclusive) through the current transformation and print the result 

Note: If there are multiple process n operations, do not overwrite the result — the original list of numbers should be preserved.

Possible Transformations:

  • double Multiply each number by 2
  • square Square each number
  • even Yield only even numbers
  • odd Yield only odd numbers

These are the ONLY possible transformations.

you may start with the given code:

# TODO: implement generator functions for transformations.
def double_gen(nums):
    """Yield each number doubled."""
    # TODO
    pass

def square_gen(nums):
    """Yield each number squared."""
    # TODO
    pass

def odd_gen(nums):
    """Yield only odd numbers."""
    # TODO
    pass

def even_gen(nums):
    """Yield only even numbers."""
    # TODO
    pass

def compose_generators(gens, start_stream):
    """Chain all generators together in order."""
    # TODO: apply each generator in sequence to the stream
    pass

def main():
    m = int(input())
    ops = []
    # Mapping operation names to their generator functions
    gen_map = {
        "double": double_gen,
        "square": square_gen,
        "odd": odd_gen,
        "even": even_gen,
    }
    for _ in range(m):
        line = input().split()
        cmd = line[0]
        if cmd == "add":
            # TODO: add the generator function to ops
            pass
        elif cmd == "remove":
            # TODO: remove the first matching generator from ops
            pass
        elif cmd == "process":
            # TODO: apply all generators to numbers from 1 to n
            # and print results one per line
            pass

if __name__ == "__main__":
    main()

Input

  • An integer m
  • Followed by m operations

Constraints

  • 2 <= m <= 100
  • 1 <= n <= 1000
  • There will always be at least one transformation every input
  • There will always be at least one process n operation every input
  • It is possible to have multiple process n operation in a single input

Output

Each time you see a process n command, output the resulting stream values one per line.

 

Explanation for the sample input:

add odd
add double → ops = [odd_gen, double_gen]
process 1–5 → [1, 2, 3, 4, 5]

  • odd_gen → [1, 3, 5]

  • double_gen → [2, 6, 10]

  • prints: 2 6 10

remove odd → ops = [double_gen]
process 1–5:

  • double_gen → [2, 4, 6, 8, 10]

  • prints: 2 4 6 8 10

​Final printed result:

2
6
10
2
4
6
8
10

Sample Input  Download

Sample Output  Download

Tags




Discuss