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.1.0.29 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.28 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.27 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.26 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.25 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.24 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.23 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.22 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.21 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.20 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.19 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.18 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.17 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.16 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.15 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.14 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.13 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.12 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.11 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.1.0.10 tracks/python/exercises/zebra-puzzle/example.py