Advertisement
Guest User

RFH

a guest
Jun 23rd, 2009
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. '''
  2. Python 3.x solution to puzzle at:
  3. http://programmingpraxis.com/2009/06/16/who-owns-the-zebra/
  4. '''
  5.  
  6. import itertools
  7.  
  8. colors = ('yellow', 'blue', 'red', 'ivory', 'green')
  9. nationalities = ('Norwegian', 'Ukranian', 'Englishman', 'Spaniard', 'Japanese')
  10. drinks = ('water', 'tea', 'milk', 'orange juice', 'coffee')
  11. brands = ('Kools', 'Chesterfields', 'Old Gold', 'Lucky Strikes', 'Parliaments')
  12. pets = ('fox', 'horse', 'snails', 'dog', 'zebra')
  13.  
  14. count = 0  # Count of solutions, just in case there is more than one.
  15.  
  16. for n in itertools.permutations(nationalities):
  17.     if n.index('Norwegian') != 0:
  18.         continue
  19.     for d in itertools.permutations(drinks):
  20.         if (d.index('milk') != 2 or
  21.             n.index('Ukranian') != d.index('tea')):
  22.             continue
  23.         for c in itertools.permutations(colors):
  24.             if (n.index('Englishman') != c.index('red') or
  25.                 d.index('coffee') != c.index('green') or
  26.                 c.index('green') != (c.index('ivory') + 1) or
  27.                 abs(n.index('Norwegian') - c.index('blue')) != 1):
  28.                 continue
  29.             for b in itertools.permutations(brands):
  30.                 if (b.index('Kools') != c.index('yellow') or
  31.                     b.index('Lucky Strikes') != d.index('orange juice') or
  32.                     n.index('Japanese') != b.index('Parliaments')):
  33.                     continue
  34.                 for p in itertools.permutations(pets):
  35.                     if (n.index('Spaniard') != p.index('dog') or
  36.                         b.index('Old Gold') != p.index('snails') or
  37.                         abs(b.index('Chesterfields') - p.index('fox')) != 1 or
  38.                         abs(b.index('Kools') - p.index('horse')) != 1):
  39.                         continue
  40.                     count +=1
  41.                     print('Solution #', count, ':', sep = '')
  42.                     print('Water drinker: ', n[d.index('water')])
  43.                     print('Zebra owner: ', n[p.index('zebra')])
  44. # End of script.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement