Duel Resolver: Speed, Types and Chip Damage
Data Structure DesignAuto-graded · 18 test casesLast updated
Simulate a two-creature duel with fixed speed order, a minimum-damage floor, and a type-advantage multiplier, then return the winner's name.
Duel Resolver
Two creatures fight in alternating attacks until one of them is knocked out. Given both creatures' stats, report who wins.
Each creature is a dictionary with these keys:
name(string)hp(hit points)atk(attack power)def(defense)spd(speed)type(one of"Fire","Water","Grass")
Rules
Who strikes first. The creature with the larger spd attacks first. If the speeds are equal, the creature passed as the first argument attacks first. Speeds never change, so the attack order is the same for the whole duel: first mover, then second mover, then first mover again, and so on.
Damage of one attack.
- Compute
base = max(1, (attacker_atk - defender_def) * 0.5). The floor of 1 is applied here, before any type bonus. - If the attacker's type has the advantage over the defender's type, multiply
baseby1.5. Otherwise multiply by1.0.
Type advantages. Fire beats Grass, Grass beats Water, and Water beats Fire. Those are the only advantages. There is no penalty for a disadvantage, and two creatures of the same type get no bonus.
Applying damage. Keep HP as a floating-point number. After each attack, set the defender's HP to round(hp - damage, 2). Do not truncate fractional damage.
Ending the duel. Check for a knockout after every single attack. A creature with HP <= 0 is knocked out and never gets to attack again, even if it would have acted later in the same round.
- If only the second creature is knocked out, return the first creature's
name. - If only the first creature is knocked out, return the second creature's
name. - If both are at
<= 0at the same moment, return"Draw".
Return the winner's name as a string, or "Draw".
Constraints
hpis greater than 0 for both creatures at the start.atk,defandspdare non-negative numbers.typeis always one of"Fire","Water","Grass".- Every attack deals at least 1 damage, so the duel always ends.
Examples
Example 1
Input: run_duel({'name': 'A', 'hp': 20, 'atk': 10, 'def': 5, 'spd': 10, 'type': 'Fire'}, {'name': 'B', 'hp': 20, 'atk': 8, 'def': 4, 'spd': 8, 'type': 'Grass'})
Output: 'A'
Example 2
Input: run_duel({'name': 'Water', 'hp': 12, 'atk': 9, 'def': 2, 'spd': 5, 'type': 'Water'}, {'name': 'Fire', 'hp': 15, 'atk': 7, 'def': 3, 'spd': 5, 'type': 'Fire'})
Output: 'Water'
Example 3
Input: run_duel({'name': 'X', 'hp': 5, 'atk': 3, 'def': 1, 'spd': 4, 'type': 'Grass'}, {'name': 'Y', 'hp': 5, 'atk': 3, 'def': 1, 'spd': 4, 'type': 'Grass'})
Output: 'X'
Hints
Hint 1
The attack order depends only on speed, so decide it once before the loop instead of re-checking every turn.
Hint 2
Write a small helper that returns the damage of one attack. Apply the minimum of 1 first, then the 1.5 multiplier, because the order changes the result.
Hint 3
Check for a knockout after every attack, not once per round, so a creature that is already down never strikes back. Remember to round HP to 2 decimals after each hit.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a deterministic simulation, so the work is translating the rules faithfully.
- Fix the attack order once. Speed never changes. If
fighter_a["spd"] >= fighter_b["spd"], thenfighter_agoes first, otherwisefighter_bdoes. Using>=gives ties to the first argument. - Compute damage with a helper.
base = max(1, (atk - def) * 0.5), then multiply by 1.5 if the attacker's type beats the defender's type (Fire over Grass, Grass over Water, Water over Fire). The floor comes before the multiplier, so a weak Fire attacker against Grass still deals1 * 1.5 = 1.5. Same-type matchups and disadvantaged matchups use a multiplier of 1.0. - Loop over single attacks. Alternate attackers in the fixed order. After each attack, update the defender's HP as
round(hp - damage, 2), then immediately check for a knockout and return if there is one.
Why it is correct
- The attack order matches the speed and tie-break rule.
- The damage follows the stated order of operations, and decimals are kept, so HP values match exactly what the rules describe.
- Checking after every attack means a knocked-out creature never acts again. This is why the second mover's attack is skipped when the first mover already won.
- The loop terminates because every attack removes at least 1 HP and HP starts finite and positive.
- Only one creature loses HP per attack, so with these rules a simultaneous knockout cannot actually occur. The
"Draw"check is still included because the specification defines it, and it costs nothing.
Complexity
- Time: O(hp_a + hp_b) in the worst case, because each attack removes at least 1 HP and each attack costs O(1).
- Space: O(1).
Reference solution
from typing import Dict
def run_duel(fighter_a: Dict[str, object], fighter_b: Dict[str, object]) -> str:
fighters = [fighter_a, fighter_b]
hp = [float(fighter_a["hp"]), float(fighter_b["hp"])]
beats = {"Fire": "Grass", "Grass": "Water", "Water": "Fire"}
def hit_damage(attacker, defender):
base = max(1.0, (attacker["atk"] - defender["def"]) * 0.5)
bonus = 1.5 if beats[attacker["type"]] == defender["type"] else 1.0
return base * bonus
# Speed never changes, so the order is fixed; ties favor fighter_a.
if fighter_a["spd"] >= fighter_b["spd"]:
order = [0, 1]
else:
order = [1, 0]
while True:
for attacker_idx in order:
defender_idx = 1 - attacker_idx
dmg = hit_damage(fighters[attacker_idx], fighters[defender_idx])
hp[defender_idx] = round(hp[defender_idx] - dmg, 2)
if hp[0] <= 0 and hp[1] <= 0:
return "Draw"
if hp[0] <= 0:
return str(fighter_b["name"])
if hp[1] <= 0:
return str(fighter_a["name"])