Daily Challenge - Character Battle (Advice

Instead of writing bad code that works, I’ve been trying to write more readable code. However sometimes is it not necessary or doing ‘too much’? Additionally how do programmers read the problem and create an efficient solution without simply spamming for loops?

import string

def battle(my_army, opposing_army) -> str:
    if len(my_army) != len(opposing_army):
        return retreat(my_army, opposing_army)

    win_count = 0
    my_list = list(map(strength, my_army))
    opposing_list = list(map(strength, opposing_army))

    battle_result = list(map(lambda x, y: x-y, my_list, opposing_list))

    win_count += sum((result > 0) - (result < 0) for result in battle_result)

    if win_count > 0:
        return "We won"
    elif win_count < 0:
        return "We lost"
    else:
        return "It was a tie"


def strength(char: str) -> int:
    ALPHABET: str = "$" + string.ascii_letters
    DIGITS: str = string.digits

    if char in ALPHABET:
        return ALPHABET.index(char)
    elif char in DIGITS:
        return DIGITS.index(char)
    else:
         return 0

def retreat(my_string: str, opposing_string: str) -> str:
    len_my_string = len(my_string)
    len_opposing_string = len(opposing_string)

    if len_my_string > len_opposing_string:
        return "Opponent retreated"
    elif len_my_string < len_opposing_string:
        return "We retreated"

print(battle("Hello", "World"))




I mean, this is a pretty wide question. It’s a coder’s lifelong mission to get better and better at elegant, readable solutions to problems. Nothing can replace practice, of course. Just keep doing exactly what you’re doing: solving the problems as efficiently and elegantly as you can at your current skill level and you’ll get better faster than you realize!

Happy coding!

Writing readable, maintainable code is almost always worth it — because you’re not just coding for the computer, you’re coding for the future you (and your teammates). Sometimes, yes, you can over-engineer or abstract too much, but in practice beginners lean more toward not enough structure rather than too much. So it’s a good instinct to aim for clarity. A simple rule: if a future reader can understand what your code is doing in one pass without mental gymnastics, you’re in a good place.

“Too much” is usually when your solution introduces layers of abstraction, helper functions, or patterns that don’t actually make the problem easier to solve or the code easier to read. For example, making five helper functions for a tiny three-line algorithm might be overkill. On the other hand, pulling repeated logic into a helper is usually a win.