| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 14815 | Math is not Fun |
|
| 14817 | Battle Simulator |
|
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_nameAdd a new transformation to the end of the streamremove transofrmation_nameRemove the first occurrence of the transformationprocess nProcess 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:
doubleMultiply each number by 2squareSquare each numberevenYield only even numbersoddYield only odd numbers
These are the ONLY possible transformations.
you may start with the given code:
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
moperations
Constraints
2 <= m <= 1001 <= n <= 1000- There will always be at least one
transformationevery input - There will always be at least one
process noperation every input - It is possible to have multiple
process noperation 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:
6
10
2
4
6
8
10
Sample Input Download
Sample Output Download
Tags
Discuss
Description
This year NTHU's I2P class is divided into two classes (Class A and Class B). To celebrate our first midterm the Domo decided to host a tournament between the 2 classes. Each class chooses one champion to represent them in a turn-based duel.

You are to simulate a turn-based battle between two characters.
Each character has three basic attributes:
-
HP (hit points) — health of the character.
-
Attack — determines how much damage they can deal.
-
Defense — reduces incoming damage.
Characters can receive buffs that modify these attributes before the battle starts.
Each buff can modify attack, defense, or HP using operations such as addition or multiplication.
You must implement a function composition system so that multiple buffs can be combined into a single function.
List of possible buffs are:
"atk_add val": increases attack by val"atk_mul val": multiplies attack by val"dfs_add val": increases defense by val"dfs_mul val": multiplies defense by val
For example if you have the given buffs:
atk_mul 2
atk_add 3
atk_mul 0.2
your new F function will be
Given the code:
def __init__(self, name, hp, attack, defense):
self.name = name
self.hp = hp
self.attack = attack
self.defense = defense
self.buff_func = lambda atk, dfs, hp: (atk, dfs, hp) # identity function
def apply_buffs(self):
"""
#TODO:
Apply the composed buff function to (attack, defense, hp).
Returns the updated (attack, defense, hp).
"""
pass
def is_alive(self):
#TODO: Return True if HP > 0.
pass
def take_damage(self, amount):
#TODO: Reduce HP by damage
pass
def attack_target(self, other):
#TODO: Attack another character
pass
def compose_buffs(*funcs):
"""Compose multiple buffs into one function."""
pass
def buff_factory(cmd, x):
"""Return a buff function that modifies (atk, dfs, hp)."""
if cmd == "atk_add":
pass
elif cmd == "atk_mul":
pass
elif cmd == "dfs_add":
pass
elif cmd == "dfs_mul":
pass
else:
pass
def battle(c1, c2):
"""
#TODO
Turn-based battle: c1 attacks first, alternate until one dies.
returns the winner's name and hp
"""
pass
if __name__ == "__main__":
# Input format:
# name1 hp1 atk1 dfs1
# name2 hp2 atk2 dfs2
# n
# Then n lines:
# target cmd value
# (target is either name1 or name2)
n1, h1, a1, d1 = input().split()
n2, h2, a2, d2 = input().split()
c1 = Character(n1, int(h1), int(a1), int(d1))
c2 = Character(n2, int(h2), int(a2), int(d2))
n = int(input())
buffs_c1 = []
buffs_c2 = []
for _ in range(n):
target, cmd, val = input().split()
buff = buff_factory(cmd, float(val))
if target == c1.name:
buffs_c1.append(buff)
elif target == c2.name:
buffs_c2.append(buff)
c1.buff_func = compose_buffs(*buffs_c1)
c2.buff_func = compose_buffs(*buffs_c2)
winner, hp_left = battle(c1, c2)
print(f"{winner} wins with {hp_left:.1f} HP left.")
Simulate the Battle Between the 2 characters given the following rules:
-
The battle alternates turns:
-
The first character (
c1) attacks first. -
Then the second character (
c2) attacks, and so on.
-
-
When a character’s HP becomes
0or below, the battle ends. -
Output the winner’s name and their remaining HP (1 decimal place).
-
Buffs will be applied EVERY round before attacking the other character (BUFF WILL STACK THROUGH THE ROUNDS)
intial attack : 20
buff : attack_add 5
Round 1: attack = 20 + 5 =25
Round 2: attack = 25 + 5 = 30
Round 3: attack = 30 + 5 = 35
-
The damage taken for the character will be
max(1, attackers.attack - receiver.defense)"APPLY BUFF FIRST BEFORE ATTACKING"
Please refer to the following link to see the round by round break down explanation of the sample input if you are confused:
Link
Input
- The first two line describe our 2 characters
<name1> <hp1> <attack1> <defense1>
<name2> <hp2> <attack2> <defense2> - Followed by an integer
n - the next n lines describes the buffs
<target1> <cmd1> <val1>
<target2> <cmd2> <val2>
...
<targetn> <cmdn> <valn>
target— the character’s name receiving the buff
cmd— one of the buff commands (e.g.,atk_add,hp_mul, etc.)
val— a floating-point or integer value
Constraints
-
1 ≤ hp, attack, defense ≤ 1000 -
0 ≤ n ≤ 20 -
valcan be integer or float (1 ≤ val ≤ 10) -
Character names are unique and contain only letters.
Output
Output the winner's name and his/her hp with the following format
<winner_name> wins with <remaining_hp> HP left.