Skip to content

Instantly share code, notes, and snippets.

@sword-jin
Last active March 30, 2016 08:33
Show Gist options
  • Select an option

  • Save sword-jin/d95274d884308711ce4cd3ec9e4df274 to your computer and use it in GitHub Desktop.

Select an option

Save sword-jin/d95274d884308711ce4cd3ec9e4df274 to your computer and use it in GitHub Desktop.
Floor Puzzle
#------------------
# User Instructions
#
# Hopper, Kay, Liskov, Perlis, and Ritchie live on
# different floors of a five-floor apartment building.
#
# Hopper does not live on the top floor.
# Kay does not live on the bottom floor.
# Liskov does not live on either the top or the bottom floor.
# Perlis lives on a higher floor than does Kay.
# Ritchie does not live on a floor adjacent to Liskov's.
# Liskov does not live on a floor adjacent to Kay's.
#
# Where does everyone live?
#
# Write a function floor_puzzle() that returns a list of
# five floor numbers denoting the floor of Hopper, Kay,
# Liskov, Perlis, and Ritchie.
import itertools
def floor_puzzle():
floors = bottom, _, _, _, top = [1,2,3,4,5]
orders = itertools.permutations(floors)
for Hopper, Kay, Liskov, Perlis, Ritchie in orders:
if (Hopper != top
and Kay != bottom
and Liskov != top and Liskov != bottom
and Perlis - Kay > 0
and abs(Ritchie - Liskov) != 1
and abs(Liskov - Kay) != 1):
return [Hopper, Kay, Liskov, Perlis, Ritchie]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment