Sha256: 93bff4933b84c775aef9557a4b64f6e9d6b1fc92d5763636308ee6372972757c

Contents?: true

Size: 1.67 KB

Versions: 127

Compression:

Stored size: 1.67 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

127 entries across 127 versions & 1 rubygems

Version Path
trackler-2.0.8.27 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.26 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.24 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.23 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.22 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.21 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.20 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.19 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.18 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.17 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.16 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.15 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.14 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.13 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.12 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.11 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.10 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.9 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.8 tracks/python/exercises/zebra-puzzle/example.py
trackler-2.0.8.7 tracks/python/exercises/zebra-puzzle/example.py