|
| 1 | +# create a list of 4 lists - each sub-list is of size 4 |
| 2 | +# Brute Force: make combinations and verify it against verification function |
| 3 | + |
| 4 | +# verification function is based on below mentioned TEN COMMANDMENTS |
| 5 | + |
| 6 | +""" |
| 7 | +TEN COMMANDMENTS: |
| 8 | +
|
| 9 | +0 The customer on Maxwell Street received the Amplifier. |
| 10 | +1 The Elephant arrived in North Avenue; |
| 11 | +6 Frank received a Doorknob. |
| 12 | +4 George's package went to Kirkwood Street ====> G does not live on K |
| 13 | +
|
| 14 | +2 the customer who ordered the Banister received the package that Irene had ordered. |
| 15 | +3 The delivery that should have gone to Kirkwood Street was sent to Lake Avenue. |
| 16 | +5 The customer who ordered the Candelabrum received the Banister, while |
| 17 | +
|
| 18 | +7 the person who had ordered Elephant received the package that should have gone to Maxwell Street. |
| 19 | +
|
| 20 | +8 Heather received the package that was to go to Orange Drive. |
| 21 | +
|
| 22 | +9 Jerry received Heather's order. |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +# ASSUMPTION: Positions of streets is fixed. |
| 27 | +# LOGIC: just check if the triplet (WRONG DELIVERY, NAME, ACTUAL ORDER) is at a place |
| 28 | +# where above mentioned conditions are not violated. |
| 29 | +def verifyState (iWorld2): |
| 30 | + |
| 31 | + # A cannot be on M |
| 32 | + if ((iWorld2[2][2] != " " and iWorld2[2][2] == "A") or (iWorld2[1][2] != " ") and iWorld2[1][2] != "A"): |
| 33 | +# print "1" |
| 34 | + return False |
| 35 | + |
| 36 | + # E cannot be on N |
| 37 | + if ((iWorld2[2][3] != " " and iWorld2[2][3] == "E") or (iWorld2[1][3] != " " and iWorld2[1][3] != "E")): |
| 38 | + # print "2" |
| 39 | + return False |
| 40 | + |
| 41 | + # F has not ordered D |
| 42 | + iList3 = iWorld2[3] |
| 43 | + idx = iList3.index("F") if "F" in iList3 else None |
| 44 | + if (idx is not None and iWorld2[2][idx] == "D"): |
| 45 | + # print "3" |
| 46 | + return False |
| 47 | + |
| 48 | + # G cannot live on K |
| 49 | + if (iWorld2[3][0] != " " and iWorld2[3][0] == "G"): |
| 50 | + # print "4" |
| 51 | + return False |
| 52 | + |
| 53 | + # H cannot live on O |
| 54 | + if (iWorld2[3][4] != " " and iWorld2[3][4] == "H"): |
| 55 | + # print "5" |
| 56 | + return False |
| 57 | + |
| 58 | + # E cannot be on M |
| 59 | + if (iWorld2[2][2] != " " and iWorld2[2][2] == "E"): |
| 60 | + # print "6" |
| 61 | + return False |
| 62 | + |
| 63 | + # I has not ordered B |
| 64 | + iList3 = iWorld2[3] |
| 65 | + idx = iList3.index("I") if "I" in iList3 else None |
| 66 | + if (idx is not None and iWorld2[2][idx] == "B"): |
| 67 | + # print "7" |
| 68 | + return False |
| 69 | + |
| 70 | + # I's order was delivered to Owner of B |
| 71 | + iList1 = iWorld2[1] # Wrong |
| 72 | + iList2 = iWorld2[2] # Ordered |
| 73 | + iList3 = iWorld2[3] # Name |
| 74 | + idxI = iList3.index("I") if "I" in iList3 else None |
| 75 | + if idxI is not None: |
| 76 | + idxPI = iList2[idxI] |
| 77 | + idxB = iList2.index("B") if "B" in iList2 else None |
| 78 | + if (idxB is not None and iList1[idxB] != idxPI): |
| 79 | + # print "8" |
| 80 | + return False |
| 81 | + |
| 82 | + # B should be delivered where C was ordered |
| 83 | + iList1 = iWorld2[1] # Wrong |
| 84 | + iList2 = iWorld2[2] # Ordered |
| 85 | + iList3 = iWorld2[3] # Name |
| 86 | + idxI = iList2.index("C") if "C" in iList2 else None |
| 87 | + if (idxI is not None and iList1[idxI] != "B"): |
| 88 | + # print "9" |
| 89 | + return False |
| 90 | + |
| 91 | + # K's order went to L |
| 92 | + iList1 = iWorld2[1] # Wrong |
| 93 | + iList2 = iWorld2[2] # Ordered |
| 94 | + iList3 = iWorld2[3] # Name |
| 95 | + idxI = iList2.index("C") if "C" in iList2 else None |
| 96 | + if (idxI is not None and iList2[0] != iList1[1]): |
| 97 | + # print "10" |
| 98 | + return False |
| 99 | + |
| 100 | + # H's order was delivered to J |
| 101 | + iList1 = iWorld2[1] # Wrong |
| 102 | + iList2 = iWorld2[2] # Ordered |
| 103 | + iList3 = iWorld2[3] # Name |
| 104 | + idxH = iList3.index("H") if "H" in iList3 else None |
| 105 | + idxJ = iList3.index("J") if "J" in iList3 else None |
| 106 | + if (idxH is not None and idxJ is not None): |
| 107 | + if (iList2[idxH] != iList1[idxJ]): |
| 108 | + # print "11" |
| 109 | + return False |
| 110 | + |
| 111 | + # G's order was delivered to K |
| 112 | + iList1 = iWorld2[1] # Wrong |
| 113 | + iList2 = iWorld2[2] # Ordered |
| 114 | + iList3 = iWorld2[3] # Name |
| 115 | + idxH = iList3.index("G") if "G" in iList3 else None |
| 116 | + if (idxH is not None): |
| 117 | + if (iList2[idxH] != iList1[0]): |
| 118 | + # print "13" |
| 119 | + return False |
| 120 | + |
| 121 | + # G's order was delivered to K |
| 122 | + iList1 = iWorld2[1] # Wrong |
| 123 | + iList2 = iWorld2[2] # Ordered |
| 124 | + iList3 = iWorld2[3] # Name |
| 125 | + idxH = iList2.index("C") if "C" in iList2 else None |
| 126 | + if (idxH is not None): |
| 127 | + if ("B" != iList1[idxH]): |
| 128 | + # print "14" |
| 129 | + return False |
| 130 | + |
| 131 | + # The person who had ordered E received the package that should have gone to M. |
| 132 | + iList1 = iWorld2[1] # Wrong |
| 133 | + iList2 = iWorld2[2] # Ordered |
| 134 | + iList3 = iWorld2[3] # Name |
| 135 | + idxH = iList2.index("E") if "E" in iList2 else None |
| 136 | + if (idxH is not None): |
| 137 | + if (iList2[2] != iList1[idxH]): |
| 138 | + # print "14" |
| 139 | + return False |
| 140 | + |
| 141 | + # F received a D. |
| 142 | + iList1 = iWorld2[1] # Wrong |
| 143 | + iList2 = iWorld2[2] # Ordered |
| 144 | + iList3 = iWorld2[3] # Name |
| 145 | + idxH = iList3.index("F") if "F" in iList3 else None |
| 146 | + if (idxH is not None): |
| 147 | + if (iList1[idxH] != "D"): |
| 148 | + # print "15" |
| 149 | + return False |
| 150 | + |
| 151 | +# print "16" |
| 152 | + return True |
| 153 | + |
| 154 | + |
| 155 | +def printProperly(solutionMatrix): |
| 156 | + symbolTable = { |
| 157 | + "A" : "Amplifier", |
| 158 | + "B" : "Banister", |
| 159 | + "C" : "Candelabrum", |
| 160 | + "D" : "Doorknob", |
| 161 | + "E" : "Elephant", |
| 162 | + "F" : "Frank", |
| 163 | + "G" : "George", |
| 164 | + "H" : "Heather", |
| 165 | + "I" : "Irene", |
| 166 | + "J" : "Jerry", |
| 167 | + "K" : "Kirkwood Steet", |
| 168 | + "L" : "Lake Avenue", |
| 169 | + "M" : "Maxwell Street", |
| 170 | + "N" : "North Avenue", |
| 171 | + "O" : "Orange Drive" |
| 172 | + } |
| 173 | + i = 0 |
| 174 | + while (i<5): |
| 175 | + output = symbolTable[solutionMatrix[3][i]] + " lives on " + symbolTable[solutionMatrix[0][i]] + " and actually ordered " + symbolTable[solutionMatrix[2][i]] + "." # + " BUT " + symbolTable[solutionMatrix[1][i]] + " was delivered!" |
| 176 | + print output |
| 177 | + i = i+1 |
| 178 | + |
| 179 | + |
| 180 | +def getThingsDone(world, deliverList, personsList, ordersList, i, dd , oo , pp, count): |
| 181 | + if (i<5): |
| 182 | + for d in deliverList: |
| 183 | + world[1][i] = d |
| 184 | + for o in ordersList: |
| 185 | + world[2][i] = o |
| 186 | + for p in personsList: |
| 187 | + world[3][i] = p |
| 188 | + getThingsDone(world, list(set(deliverList)-set(world[1][dd])), list(set(personsList)-set(world[3][pp])), list(set(ordersList)-set(world[2][oo])), 1+i, 1+dd, 1+oo, 1+pp, count) |
| 189 | + else: |
| 190 | + status = verifyState(world) |
| 191 | + if (status): |
| 192 | + print "\n" |
| 193 | + printProperly(world) |
| 194 | + print "\n" |
| 195 | + return |
| 196 | + |
| 197 | +def main(): |
| 198 | + # INITIAL STATE: We do not know any correct position initially. We just start filling and at the same time verify our combination against the 10 rules mentioned in the question |
| 199 | + world1 = [ |
| 200 | + # ["0","1","2","3","4"] |
| 201 | + ["K","L","M","N","O"], # 0 FIXED STREET |
| 202 | + [" "," "," "," "," "], # 1 STREET WRONG DELIVERY |
| 203 | + [" "," "," "," "," "], # 2 ORDER |
| 204 | + [" "," "," "," "," "] # 3 PERSON |
| 205 | + ] |
| 206 | + |
| 207 | + # swap-fill-verify until it is True is received |
| 208 | + deliverList = set(["A","B","C","D","E"]) # constant |
| 209 | + personsList = set(["F","G","H","I","J"]) # constant |
| 210 | + ordersList = set(["A","B","C","D","E"]) # constant |
| 211 | + |
| 212 | + getThingsDone(world1, deliverList, personsList, ordersList, 0, 0 , 0 , 0, 0) |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +main() |
| 217 | + |
0 commit comments