Skip to content

Commit 84af2c6

Browse files
committed
feat(day 14): add character battle simultator with alphabett and digit strength logic
1 parent 8e7bd50 commit 84af2c6

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

FreeCodeCamp/CodingQ/CharBattle.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
3+
===============================> Character Battle <=====================================================
4+
5+
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:
6+
7+
Characters a-z have a strength of 1-26, respectively.
8+
Characters A-Z have a strength of 27-52, respectively.
9+
Digits 0-9 have a strength of their face value.
10+
All other characters have a value of zero.
11+
Each character can only fight one battle.
12+
For each battle, the stronger character wins. The army with more victories, wins the war. Return the following values:
13+
14+
"Opponent retreated" if your army has more characters than the opposing army.
15+
"We retreated" if the opposing army has more characters than yours.
16+
"We won" if your army won more battles.
17+
"We lost" if the opposing army won more battles.
18+
"It was a tie" if both armies won the same number of battles.
19+
20+
==================================================================================
21+
O/P :
22+
23+
1. battle("Hello", "World") should return "We lost".
24+
2. battle("pizza", "salad") should return "We won".
25+
"""
26+
27+
28+
29+
def getStrength(char):
30+
31+
if 'a' <= char <= 'z':
32+
return ord(char) - ord('a') + 1
33+
elif 'A' <= char <= 'Z':
34+
return ord(char) - ord('A') + 27
35+
elif char.isdigit():
36+
return int(char)
37+
else:
38+
return 0
39+
40+
41+
def battle(my_army,opposing_army):
42+
43+
if len(my_army) > len(opposing_army):
44+
return "Opponent retreated"
45+
elif len(my_army) < len(opposing_army):
46+
return "We retreated"
47+
48+
my_wins = 0
49+
opp_wins = 0
50+
51+
for my_char,opp_char in zip(my_army,opposing_army):
52+
my_strength = getStrength(my_char)
53+
opp_strength = getStrength(opp_char)
54+
55+
if my_strength > opp_strength:
56+
my_wins += 1
57+
elif my_strength < opp_strength:
58+
opp_wins += 1
59+
60+
if my_wins > opp_wins:
61+
return "We won"
62+
elif opp_wins > my_wins:
63+
return "We lost"
64+
else:
65+
return "It was a tie"
66+
67+
68+
69+
# Assuming your battle() function is already defined above
70+
71+
assert battle("Hello", "World") == "We lost"
72+
assert battle("pizza", "salad") == "We won"
73+
assert battle("C@T5", "D0G$") == "We won"
74+
assert battle("kn!ght", "orc") == "Opponent retreated"
75+
assert battle("PC", "Mac") == "We retreated"
76+
assert battle("Wizards", "Dragons") == "It was a tie"
77+
assert battle("Mr. Smith", "Dr. Jones") == "It was a tie"
78+
79+
print("All tests passed!")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from CharBattle import battle
3+
4+
class TestBattleFunction(unittest.TestCase):
5+
def test_we_lost(self):
6+
self.assertEqual(battle("Hello","World"), "We lost")
7+
8+
def test_we_won_1(self):
9+
self.assertEqual(battle("pizza", "salad"), "We won")
10+
11+
def test_we_won_2(self):
12+
self.assertEqual(battle("C@T5", "D0G$"), "We won")
13+
14+
def test_opponent_retreated(self):
15+
self.assertEqual(battle("kn!ght", "orc"), "Opponent retreated")
16+
17+
def test_we_retreated(self):
18+
self.assertEqual(battle("PC", "Mac"), "We retreated")
19+
20+
def test_tie_1(self):
21+
self.assertEqual(battle("Wizards", "Dragons"), "It was a tie")
22+
23+
def test_tie_2(self):
24+
self.assertEqual(battle("Mr. Smith", "Dr. Jones"), "It was a tie")
25+
26+
if __name__ == "__main__":
27+
unittest.main()

0 commit comments

Comments
 (0)