Battle of the characters (Easy)
Groups of characters decided to make a battle. Help them to figure out which group is more powerful. Create a function that will accept 2 variables and return the one who's stronger.
Rules:
- Each character have its own power:
A = 1, B = 2, ... Y = 25, Z = 26
- Only capital characters can and will participate a battle.
- Only two groups to a fight.
- Group whose total power (
A + B + C + ...
) is bigger wins. - If the powers are equal, it's a tie.
Example
battle_of_the_characters("ONE", "TWO"); // => "TWO"`
battle_of_the_characters("ONE", "NEO"); // => "Tie!"
battle_of_the_characters("ONE", "TWO"); // => "TWO"`
battle_of_the_characters("ONE", "NEO"); // => "Tie!"
Solution
py
def battle_of_the_characters(x, y):
x_total, y_total = 0, 0
x_total = sum(ord(x[i]) - 64 for i in range(len(x)))
y_total = sum(ord(y[i]) - 64 for i in range(len(y)))
return x if x_total > y_total else y if x_total < y_total else 'Tie!'
print(battle_of_the_characters("AAA", "Z"))