Sha256: 239cab7f8e2f1f14351610d796d8b8abaf42e199c3948b9463da01e8bff55860

Contents?: true

Size: 1.45 KB

Versions: 198

Compression:

Stored size: 1.45 KB

Contents

"""This solution for the Zebra Puzzle is adapted from a solution
by Peter Norvig for the course "Design of Computer Programs" on Udacity.
https://www.udacity.com/course/cs212
"""

from itertools import permutations


def just_right_of(x, y):
    return x - y == 1


def next_to(x, y):
    return abs(x - y) == 1


def solution():
    houses = first, _, middle, _, _ = range(5)
    orderings = list(permutations(houses))
    result = next(
        [{
            Englishman: "Englishman",
            Spaniard: "Spaniard",
            Ukranian: "Ukranian",
            Japanese: "Japanese",
            Norwegian: "Norwegian"
        }[x] for x in (water, zebra)]
        for (red, green, ivory, yellow, blue) in orderings
        if just_right_of(green, ivory)
        for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings
        if Englishman is red if Norwegian is first if next_to(Norwegian, blue)
        for (coffee, tea, milk, oj, water) in orderings if coffee is green
        if Ukranian is tea if milk is middle
        for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments
             ) in orderings if Kools is yellow if LuckyStrike is oj
        if Japanese is Parliaments
        for (dog, snails, fox, horse, zebra) in orderings if Spaniard is dog
        if OldGold is snails if next_to(Chesterfields, fox)
        if next_to(Kools, horse))
    return ("It is the {} who drinks the water.\n"
            "The {} keeps the zebra.").format(*result)

Version data entries

198 entries across 198 versions & 1 rubygems

Version Path
trackler-2.0.8.45 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.44 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.43 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.42 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.41 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.40 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.39 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.38 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.37 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.36 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.35 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.34 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.33 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.32 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.31 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.30 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.29 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.28 tracks/python/exercises/zebra-puzzle/example.py