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
Grundl has guarded the only bridge over the Skarn for three hundred winters. He charges every traveller a toll, and he changes his rules whenever it suits him. Travellers who cannot reckon the toll to the coin go into the river. To cross, you will write the toll-keeper's reckoning so plainly that even a troll cannot fault it.

The fight

Four stages, each building on the last. Every stage opens with your finished 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: The toll board. Price one traveller.
Stage 2: The queue. Price a whole queue, carts and all.
Stage 3: The tally. Count who crossed and find the busiest kind.
Stage 4: The reckoning. Write it all up, line by line.

The fight

0 of 4 stages won

Stage 1 of 4

+60 XP
Grundl's toll board reads: a 'walker' pays 2 coins, a 'horse' 3, a 'cart' 5, and anything else pays 10, because Grundl hates surprises. He reads the board exactly, so 'Walker' with a capital is not a walker. Write a function toll(kind) that returns the toll for one traveller of that kind. For example, toll('horse') returns 3 and toll('goat') returns 10.
def toll(kind):
    # Return the toll in coins for one traveller of this kind
    pass

Run your code to check it against the tests.

Stage 2 of 4

+60 XP
Carts wear the bridge down, so Grundl has a new rule: when there are more than 3 carts in a queue, every cart in it pays double. Keep toll and write total_toll(travellers), where travellers is a list of kinds; it returns what the whole queue pays. Use toll for each traveller's price. For example, total_toll(['walker', 'cart']) is 7, and total_toll(['cart', 'cart', 'cart', 'cart']) is 40.
def toll(kind):
    if kind == 'walker':
        return 2
    elif kind == 'horse':
        return 3
    elif kind == 'cart':
        return 5
    else:
        return 10




def total_toll(travellers):
    # Count the carts first, then add up every traveller's toll,
    # doubling each cart's toll when there are more than 3 carts
    pass

Run your code to check it against the tests.

Stage 3 of 4

+60 XP
Grundl wants to know who uses his bridge. Keep everything and write two functions. tally(travellers) returns a dictionary mapping each kind to how many of that kind are in the list, with the kinds in the order they first appear. busiest(counts) takes such a dictionary and returns the kind with the highest count; when two kinds tie, the one that comes first in the dictionary wins, and an empty dictionary gives None. For example, tally(['cart', 'walker', 'cart']) is {'cart': 2, 'walker': 1} and busiest({'cart': 2, 'walker': 1}) is 'cart'.
def toll(kind):
    if kind == 'walker':
        return 2
    elif kind == 'horse':
        return 3
    elif kind == 'cart':
        return 5
    else:
        return 10




def total_toll(travellers):
    carts = 0
    for kind in travellers:
        if kind == 'cart':
            carts += 1
    total = 0
    for kind in travellers:
        price = toll(kind)
        if kind == 'cart' and carts > 3:
            price = price * 2
        total += price
    return total




def tally(travellers):
    # Map each kind to how many times it appears
    pass




def busiest(counts):
    # Return the kind 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
Now write Grundl's reckoning. Keep everything and write reckoning(travellers), which returns a list of lines. There is one line per kind, in the order the kinds first appear, formatted as f'{count} x {kind}: {paid}', where paid is what those travellers paid under the stage 2 rules. The last line is f'Total: {total}'. For example, reckoning(['walker', 'cart', 'walker']) is ['2 x walker: 4', '1 x cart: 5', 'Total: 9'], and an empty queue gives ['Total: 0'].
def toll(kind):
    if kind == 'walker':
        return 2
    elif kind == 'horse':
        return 3
    elif kind == 'cart':
        return 5
    else:
        return 10




def total_toll(travellers):
    carts = 0
    for kind in travellers:
        if kind == 'cart':
            carts += 1
    total = 0
    for kind in travellers:
        price = toll(kind)
        if kind == 'cart' and carts > 3:
            price = price * 2
        total += price
    return total




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




def busiest(counts):

Run your code to check it against the tests.