Legends ofPythos
Claim your name
Python Basics

Grundl, the Bridge Troll

The last fight of this track

Boss · level 6

Grundl

the Bridge Troll

Health4 of 4 stages standing
For three hundred winters, Grundl has blocked the only bridge over the Skarn, and no one has crossed. He will not stand aside, so you will have to fight your way through. Your code is your weapon. Every stage you win lands a blow; every run that fails brings his club down.

The fight

Four stages, each building on the last. Every stage opens with a finished version of the code from the stage before, so you pick up where you left off. This fight draws on functions, conditions, loops, lists, dictionaries and f-strings.
Stage 1: Duck and roll. Answer each of Grundl's moves.
Stage 2: Strike back. Add up the damage you deal.
Stage 3: His habits. Count his moves and find his favourite.
Stage 4: The last blow. Plan the finish, line by line.

The fight

0 of 4 stages won

Stage 1 of 4

+60 XP
Grundl fights with three moves, and each one has an answer. When he swings his club sideways ('swing'), you 'duck'. When he slams it down ('slam'), you 'roll' aside. When he charges ('charge'), you 'sidestep'. For any other move, you 'block': never guess against a troll. Python compares text exactly, so 'Swing' with a capital is not a swing. Write a function counter(move) that returns your answer to one move (return it; don't print it). For example, counter('slam') returns 'roll' and counter('stomp') returns 'block'.
def counter(move):
    # Return your answer to one of Grundl's moves
    pass

Run your code to check it against the tests.

Stage 2 of 4

+60 XP
Every answer leaves an opening to strike back. Use counter to find your answer to each move, then look that answer up in DAMAGE to see how hard you hit: 2 after a duck, 3 after a roll, 5 after a sidestep (his back is wide open) and 1 after a block. Charging wears a troll out: his first three charges are nothing special, but from his 4th charge on, your strike after each charge does double damage. Strikes after any other move never double, and DAMAGE itself never changes. Keep counter and write damage(moves), where moves is a list of his moves in order; it returns the total damage you deal. For example, damage(['swing', 'charge']) is 7, and damage(['charge', 'charge', 'charge', 'charge', 'swing']) is 27: the 4th charge's strike is 10, and the swing after it is still 2.
def counter(move):
    if move == 'swing':
        return 'duck'
    elif move == 'slam':
        return 'roll'
    elif move == 'charge':
        return 'sidestep'
    else:
        return 'block'




# How hard you strike back after each answer
DAMAGE = {'duck': 2, 'roll': 3, 'sidestep': 5, 'block': 1}




def damage(moves):
    # Add up your strike after each of his moves,
    # counting his charges as you go: from the 4th, a charge's strike does double
    pass

Run your code to check it against the tests.

Stage 3 of 4

+60 XP
Grundl is a creature of habit: learn his, and you know what is coming. Keep everything and write two functions. tally(moves) returns a dictionary mapping each move to how many times he made it, with the moves in the order he first made them. favourite(counts) takes such a dictionary and returns the move he made most; when two moves tie, the one that comes first in the dictionary wins, and an empty dictionary gives None. For example, tally(['swing', 'slam', 'swing']) is {'swing': 2, 'slam': 1} and favourite({'swing': 2, 'slam': 1}) is 'swing'.
def counter(move):
    if move == 'swing':
        return 'duck'
    elif move == 'slam':
        return 'roll'
    elif move == 'charge':
        return 'sidestep'
    else:
        return 'block'




# How hard you strike back after each answer
DAMAGE = {'duck': 2, 'roll': 3, 'sidestep': 5, 'block': 1}




def damage(moves):
    total = 0
    charges = 0
    for move in moves:
        hit = DAMAGE[counter(move)]
        if move == 'charge':
            charges += 1
            if charges > 3:
                hit = hit * 2
        total += hit
    return total




def tally(moves):
    # Map each move to how many times he made it
    pass




def favourite(counts):
    # Return the move with the highest count (first one wins a tie), or None
    pass

Run your code to check it against the tests.

Stage 4 of 4

+60 XP
Grundl is on his last legs, and you know his habits. Plan the finish. Keep everything and write battle_plan(moves), where moves is the list of moves he is about to make; it returns a list of lines. First comes one line for each different move, in the order he first makes it, formatted as f'{count} x {move}: {answer}', where answer is your counter to that move. If he makes any moves at all, the next line is f'Watch for: {move}', naming his favourite. The last line is always f'Damage: {total}': the damage you deal under the stage 2 rules. For example, battle_plan(['swing', 'charge', 'swing']) is ['2 x swing: duck', '1 x charge: sidestep', 'Watch for: swing', 'Damage: 9'], and an empty list gives ['Damage: 0'].
def counter(move):
    if move == 'swing':
        return 'duck'
    elif move == 'slam':
        return 'roll'
    elif move == 'charge':
        return 'sidestep'
    else:
        return 'block'




# How hard you strike back after each answer
DAMAGE = {'duck': 2, 'roll': 3, 'sidestep': 5, 'block': 1}




def damage(moves):
    total = 0
    charges = 0
    for move in moves:
        hit = DAMAGE[counter(move)]
        if move == 'charge':
            charges += 1
            if charges > 3:
                hit = hit * 2
        total += hit
    return total




def tally(moves):
    counts = {}
    for move in moves:
        if move in counts:
            counts[move] += 1
        else:
            counts[move] = 1
    return counts

Run your code to check it against the tests.