Sha256: c13f86b3a1aa54c51aa17b944173f0028107123b6e02204b15dc1a46a9ac855d

Contents?: true

Size: 1.91 KB

Versions: 42

Compression:

Stored size: 1.91 KB

Contents

def chain():

    animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]

    phrases = [" wriggled and jiggled and tickled inside her.",
               "How absurd to swallow a bird!",
               "Imagine that, to swallow a cat!",
               "What a hog, to swallow a dog!",
               "Just opened her throat and swallowed a goat!",
               "I don't know how she swallowed a cow!",
               "She's dead, of course!"]

    old_lady = "I know an old lady who swallowed a "
    swallowed = "She swallowed the <animal> to catch the "
    die = "I don't know why she swallowed the fly. Perhaps she'll die."

    song = ""
    verse = ""
    chain = ""

    for number, animal in enumerate(animals):
        verse = old_lady + animal + ".\n"

        if number == 7:
            verse += phrases[6]
        else:
            if number == 0:
                chain = swallowed + animal + '.\n'
            elif number == 1:
                verse += "It" + phrases[0] + "\n"
                chain = chain.replace("<animal>", animal)
                verse += chain
                chain = swallowed+animal+" that"+phrases[0]+"\n"+chain
            else:
                verse += phrases[number-1] + "\n"
                chain = chain.replace("<animal>", animal)
                verse += chain
                chain = swallowed + animal + ".\n" + chain

            verse += die + "\n"

        verse += "\n"
        song += verse

    return song


def verses(letter):
    return letter.replace('die.', 'die.slice').split('slice')


def recite(start_verse, end_verse):
    generated = [verse.strip().split("\n") for verse in verses(chain())]
    if start_verse == end_verse:
        return generated[start_verse-1]
    else:
        result = []
        for i in range(start_verse-1, end_verse):
            result += generated[i] + [""]

        # Pop out the last empty string
        result.pop()
        return result

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.179 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.178 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.177 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.176 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.175 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.174 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.173 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.172 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.171 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.170 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.169 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.167 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.166 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.165 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.164 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.163 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.162 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.161 tracks/python/exercises/food-chain/example.py
trackler-2.2.1.160 tracks/python/exercises/food-chain/example.py