Legends ofPythos
Claim your name
Objects and Classes

Loki, the Shapeshifter

The last fight of this track

Boss · level 15

Loki

the Shapeshifter

Health4 of 4 stages standing
The Bifröst, the bridge of light, is the only way on, and Loki has taken it. He cheated Brokk the smith out of a wager he had won, and in the end the gods chained him, but he slipped free, as he always does. Now the bridge shimmers with copies of him: a salmon, a mare, a fly, a hundred Lokis, and only one is real. Model every form he takes, and the copies fail. Your code is your net. Every stage you win draws it tighter; every run that fails sends his fire at you.

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 classes, inheritance, super(), your own iterators, properties and dataclasses.
Stage 1: The first form. Model one of his forms, so you can tell it from the rest.
Stage 2: The shapes. Learn how the salmon, the mare and the fly each move.
Stage 3: Every copy. Hold every form he has, walk through them all, and call one out by name.
Stage 4: The binding. Write the real Loki, and hold him to a rule he can't break.

The fight

0 of 4 stages won

Stage 1 of 4

+60 XP
Every copy on the bridge wears one of his forms. Model a form exactly, and you can pick it out of any crowd. Write a class Form whose __init__(self, name, speed) stores both values as the attributes name and speed. Give it a method move() that returns f'{self.name} moves at {self.speed}', a __repr__ that returns f'Form({self.name!r}, {self.speed!r})' (the !r shows each value the way repr would, so strings keep their quotes), and an __eq__ that treats two forms as equal when their names and speeds match and returns NotImplemented when the other object is not a Form. For example, repr(Form('mare', 9)) is "Form('mare', 9)".
class Form:
    def __init__(self, name, speed):
        # Store name and speed
        pass


    # move, __repr__ and __eq__ go here

Run your code to check it against the tests.

Stage 2 of 4

+60 XP
Three of his copies are animals. A salmon leaps through the light, a mare paces the arch and a fly buzzes at your ear: learn how each one moves, and none of them can fool you. Keep Form and write three subclasses: Salmon, Mare and Fly. Each one's __init__ takes only speed and calls super().__init__() with its own name: 'salmon', 'mare' or 'fly'. Each overrides move(): a salmon returns f'{self.name} swims at {self.speed}', a mare f'{self.name} gallops at {self.speed}' and a fly f'{self.name} buzzes at {self.speed}'. For example, Mare(9).move() is 'mare gallops at 9', and Mare(9) == Form('mare', 9) is True.
class Form:
    def __init__(self, name, speed):
        self.name = name
        self.speed = speed


    def move(self):
        return f'{self.name} moves at {self.speed}'


    def __repr__(self):
        return f'Form({self.name!r}, {self.speed!r})'


    def __eq__(self, other):
        if not isinstance(other, Form):
            return NotImplemented
        return self.name == other.name and self.speed == other.speed




class Salmon(Form):
    # __init__(self, speed) and move
    pass




class Mare(Form):
    pass




class Fly(Form):
    pass

Run your code to check it against the tests.

Stage 3 of 4

+60 XP
Now hold all his forms at once. Walk through them as often as you like and call any one out by name. Ask for a shape he doesn't have, and he has nowhere left to hide. Keep everything and write two more classes (the exception NoSuchForm is already there). ShiftIterator takes a list of forms and hands them out one at a time: its __next__ returns the next form and raises StopIteration when there are none left, and its __iter__ returns the iterator itself. Shapeshifter takes a list of forms in __init__, supports len(), and its __iter__ returns a new ShiftIterator over its forms, so a shapeshifter can be looped over more than once. Its method shift(name) returns the first form with that name, or raises NoSuchForm if there is none. For example, [f.name for f in Shapeshifter([Salmon(3), Fly(2)])] is ['salmon', 'fly'].
class Form:
    def __init__(self, name, speed):
        self.name = name
        self.speed = speed


    def move(self):
        return f'{self.name} moves at {self.speed}'


    def __repr__(self):
        return f'Form({self.name!r}, {self.speed!r})'


    def __eq__(self, other):
        if not isinstance(other, Form):
            return NotImplemented
        return self.name == other.name and self.speed == other.speed




class Salmon(Form):
    def __init__(self, speed):
        super().__init__('salmon', speed)


    def move(self):
        return f'{self.name} swims at {self.speed}'




class Mare(Form):
    def __init__(self, speed):
        super().__init__('mare', speed)


    def move(self):
        return f'{self.name} gallops at {self.speed}'




class Fly(Form):
    def __init__(self, speed):
        super().__init__('fly', speed)

Run your code to check it against the tests.

Stage 4 of 4

+60 XP
Now write the real Loki. He is a Shapeshifter that holds every form you've modelled, and his mischief counts every trick he plays. He cheated Brokk, and he'll try to cheat that count below zero. Your setter won't let him. Keep everything. Write a dataclass Trick with the fields name: str and cost: int = 1. Then write class Loki(Shapeshifter), which starts with a mischief of 0. mischief is a property whose setter raises ValueError for a negative value. Loki's method play(trick) adds the trick's cost to his mischief through the property (so a trick that would take it below zero raises ValueError) and returns the new total. For example, a new Loki that plays Trick('mirror') and then Trick('wager', 5) gets 1 and then 6.
class Form:
    def __init__(self, name, speed):
        self.name = name
        self.speed = speed


    def move(self):
        return f'{self.name} moves at {self.speed}'


    def __repr__(self):
        return f'Form({self.name!r}, {self.speed!r})'


    def __eq__(self, other):
        if not isinstance(other, Form):
            return NotImplemented
        return self.name == other.name and self.speed == other.speed




class Salmon(Form):
    def __init__(self, speed):
        super().__init__('salmon', speed)


    def move(self):
        return f'{self.name} swims at {self.speed}'




class Mare(Form):
    def __init__(self, speed):
        super().__init__('mare', speed)


    def move(self):
        return f'{self.name} gallops at {self.speed}'




class Fly(Form):
    def __init__(self, speed):
        super().__init__('fly', speed)

Run your code to check it against the tests.