mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-09 21:01:20 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
3.4 KiB
3.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821ec98237de8297eaee79c | Python Challenge 14: Character Battle | 29 | python-challenge-14 |
--description--
Given two strings representing your army and an opposing army, each character from your army battles the character at the same position from the opposing army using the following rules:
- Characters
a-zhave a strength of1-26, respectively. - Characters
A-Zhave a strength of27-52, respectively. - Digits
0-9have a strength of their face value. - All other characters have a value of zero.
- Each character can only fight one battle.
For each battle, the stronger character wins. The army with more victories, wins the war. Return the following values:
"Opponent retreated"if your army has more characters than the opposing army."We retreated"if the opposing army has more characters than yours."We won"if your army won more battles."We lost"if the opposing army won more battles."It was a tie"if both armies won the same number of battles.
--hints--
battle("Hello", "World") should return "We lost".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("Hello", "World"), "We lost")`)
}})
battle("pizza", "salad") should return "We won".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("pizza", "salad"), "We won")`)
}})
battle("C@T5", "D0G$") should return "We won".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("C@T5", "D0G$"), "We won")`)
}})
battle("kn!ght", "orc") should return "Opponent retreated".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("kn!ght", "orc"), "Opponent retreated")`)
}})
battle("PC", "Mac") should return "We retreated".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("PC", "Mac"), "We retreated")`)
}})
battle("Wizards", "Dragons") should return "It was a tie".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("Wizards", "Dragons"), "It was a tie")`)
}})
battle("Mr. Smith", "Dr. Jones") should return "It was a tie".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(battle("Mr. Smith", "Dr. Jones"), "It was a tie")`)
}})
--seed--
--seed-contents--
def battle(my_army, oppoising_army):
return my_army
--solutions--
def get_strength(soldier):
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
if soldier.isdigit():
return int(soldier)
elif soldier in letters:
return letters.index(soldier) + 1
else:
return 0
def battle(my_army, opposing_army):
if len(my_army) > len(opposing_army):
return 'Opponent retreated'
if len(opposing_army) > len(my_army):
return 'We retreated'
my_wins = 0
their_wins = 0
for my_soldier, their_soldier in zip(my_army, opposing_army):
my_strength = get_strength(my_soldier)
their_strength = get_strength(their_soldier)
if my_strength > their_strength:
my_wins += 1
elif their_strength > my_strength:
their_wins += 1
if my_wins > their_wins:
return 'We won'
elif their_wins > my_wins:
return 'We lost'
else:
return 'It was a tie'