First time trying the daily challenges. But it seems like the tests aren’t working for me in this, despite manual runs returning the required results.
For the context of the challenge itself, with my code below:
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.Tests
Failed:1.
battle("Hello", "World")should return"We lost".Failed:2.
battle("pizza", "salad")should return"We won".Failed:3.
battle("C@T5", "D0G$")should return"We won".Failed:4.
battle("kn!ght", "orc")should return"Opponent retreated".Failed:5.
battle("PC", "Mac")should return"We retreated".Failed:6.
battle("Wizards", "Dragons")should return"It was a tie".Failed:7.
battle("Mr. Smith", "Dr. Jones")should return"It was a tie".
def battle(my_army, opposing_army):
# Assigning characters to strength levels in indexes
strength = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ,'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# Assigning values
my_army_wins = 0
opposing_army_wins = 0
# Split characters for each argument into a list
my_army_list = []
for x in my_army:
my_army_list.append(x)
opposing_army_list = []
for x in opposing_army:
opposing_army_list.append(x)
# Convert characters into numbers
def convert(list):
for i in range(len(list)):
if list[i].isalpha():
list[i] = strength.index(list[i])
elif list[i].isdigit():
list[i] = int(list[i])
else:
list[i] = 0
convert(my_army_list)
convert(opposing_army_list)
# Results for when there are more characters on one side (retreating), then find the results of the battles
if len(my_army) > len(opposing_army):
print("Opponent retreated")
elif len(my_army) < len(opposing_army):
print("We retreated")
else:
# Compare values between both arguments before results
for i in range(len(my_army_list)):
if my_army_list[i] > opposing_army_list[i]:
my_army_wins += 1
elif my_army_list[i] < opposing_army_list[i]:
opposing_army_wins += 1
else:
continue
# Continue results of battle
if my_army_wins > opposing_army_wins:
print("We won")
elif my_army_wins < opposing_army_wins:
print("We lost")
elif my_army_wins == opposing_army_wins:
print("It was a tie")
print(my_army_list)
print(opposing_army_list)
battle("Mr. Smith", "Dr. Jones")