Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Python 3.x solution to puzzle at:
- http://programmingpraxis.com/2009/06/16/who-owns-the-zebra/
- '''
- import itertools
- colors = ('yellow', 'blue', 'red', 'ivory', 'green')
- nationalities = ('Norwegian', 'Ukranian', 'Englishman', 'Spaniard', 'Japanese')
- drinks = ('water', 'tea', 'milk', 'orange juice', 'coffee')
- brands = ('Kools', 'Chesterfields', 'Old Gold', 'Lucky Strikes', 'Parliaments')
- pets = ('fox', 'horse', 'snails', 'dog', 'zebra')
- count = 0 # Count of solutions, just in case there is more than one.
- for n in itertools.permutations(nationalities):
- if n.index('Norwegian') != 0:
- continue
- for d in itertools.permutations(drinks):
- if (d.index('milk') != 2 or
- n.index('Ukranian') != d.index('tea')):
- continue
- for c in itertools.permutations(colors):
- if (n.index('Englishman') != c.index('red') or
- d.index('coffee') != c.index('green') or
- c.index('green') != (c.index('ivory') + 1) or
- abs(n.index('Norwegian') - c.index('blue')) != 1):
- continue
- for b in itertools.permutations(brands):
- if (b.index('Kools') != c.index('yellow') or
- b.index('Lucky Strikes') != d.index('orange juice') or
- n.index('Japanese') != b.index('Parliaments')):
- continue
- for p in itertools.permutations(pets):
- if (n.index('Spaniard') != p.index('dog') or
- b.index('Old Gold') != p.index('snails') or
- abs(b.index('Chesterfields') - p.index('fox')) != 1 or
- abs(b.index('Kools') - p.index('horse')) != 1):
- continue
- count +=1
- print('Solution #', count, ':', sep = '')
- print('Water drinker: ', n[d.index('water')])
- print('Zebra owner: ', n[p.index('zebra')])
- # End of script.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement