# -*- Mode: Python -*- from constraint import * from pprint import pprint as pp # 1. Either the person in twelfth place or the person in fourteenth place is agoraphobic. # 2. Of Kailey and the person who lives in Michigan, one is agoraphobic and the other came in sixteenth place. # 3. The person in seventh place is Jesus. # 4. The arachnophobic finished before the person who lives in South Dakota. # 5. The person who lives in Rhode Island finished after Robert. # 6. The hydrophobic is not Alessandra. # 7. The person in twelfth place isn't hydrophobic. # 8. The person who lives in West Virginia is agoraphobic. # 9. The coulrophobic finished before Alessandra. # 10. The 5 people were the person who lives in Michigan, the person in eleventh place, Jesus, the hydrophobic, and the person who lives in West Virginia. # the answer: #Places First Names Phobias States #seventh Jesus arachnophobic Conneticut #eleventh Robert coulrophobic South Dakota #twelfth Kailey agoraphobic West Virginia #fourteenth Erica hydrophobic Rhode Island #sixteenth Alessandra claustrophic Michigan p = Problem() # names = jesus, robert, kailey, erica, alessandra (JRKEA) # phobias = arachno, coulro, agora, hydro, claustro (abcde) # states = CT, SD, WV, RI, MI (mnopq) places = [7,11,12,14,16] p.addVariables ("JRKEAabcdemnopq", places) p.addConstraint (AllDifferentConstraint(), "JRKEA") p.addConstraint (AllDifferentConstraint(), "abcde") p.addConstraint (AllDifferentConstraint(), "mnopq") p.addConstraint (lambda c: c == 12 or c == 14, 'c') p.addConstraint (lambda K,q,c: (K==c and q==16) or (K==16 and q==c), "Kqc") p.addConstraint (lambda J: J == 7, "J") p.addConstraint (lambda a,n: a < n, "an") p.addConstraint (lambda p,R: p > R, "pR") p.addConstraint (lambda d,A: d != A, "dA") p.addConstraint (lambda d: d != 12, "d") p.addConstraint (lambda o,c: o == c, "oc") p.addConstraint (lambda b,A: b < A, "bA") def uniquep (*args): seen = [] for arg in args: if arg in seen: return False seen.append (arg) else: return True p.addConstraint (lambda q,J,d,o: uniquep (q,J,d,o,11), "qJdo") pp (p.getSolutions())