floor puzzles

  • 根据已知问题条件进行求解
#------------------
# 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 is_adjacent(a,b):
    return abs(a-b) == 1

def floor_puzzle():
    # Your code here
    for Hopper,Kay,Liskov,Perlis,Ritchie in itertools.permutations(list(range(1,6)),5):
        if Hopper == 5: continue
        if Kay == 1: continue
        if Liskov == 1 or Liskov == 5: continue
        if Perlis < Kay: continue
        if is_adjacent(Ritchie,Liskov): continue
        if is_adjacent(Liskov,Kay): continue
        return [Hopper, Kay, Liskov, Perlis, Ritchie]

你可能感兴趣的:(数据结构与算法刷题)