The wizards' duel
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
- comparisons
Problem
Magic duels in Vel are fought in rounds. Each round both wizards cast a spell at the same moment, and the stronger spell takes the round. The duel does not go to whoever threw more total power, but to whoever won more rounds.
Write a function that takes two lists of whole numbers of the same length: the first holds the power of each spell cast by wizard A, round by round, and the second holds wizard B's. Return the text "A" or "B" for whoever won more rounds, or "tie" if both won the same number.
With 3, 10, 2 against 4, 1, 5, B wins the first and third rounds and A only the second: you return "B", even though A threw 15 power and B only 10. If both spells in a round have the same power, that round belongs to nobody. Powers go from 0 up. If the lists are empty there were no rounds, and the duel is a "tie".
Examples
The example
[3, 10, 2], [4, 1, 5] → "B"
A wins with one round tied
[7, 2, 6, 5], [3, 8, 1, 5] → "A"
One round each
[4, 1], [2, 6] → "tie"
A has more power but fewer wins
[1, 1, 20], [2, 3, 4] → "B"
Two rounds with no winner
[5, 5, 5], [5, 5, 6] → "B"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def duel(power_a, power_b):
passJavaScript
function duel(powerA, powerB) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.