Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Monday, May 28, 2012

final exam CS212: Logic Puzzle

This is the second question from the Udacity CS212 final exam. I will update this article with my solution in 1 week. Please do not comment anything during the final exam week !!
"""
UNIT 2: Logic Puzzle

You will write code to solve the following logic puzzle:

1. The person who arrived on Wednesday bought the laptop.
2. The programmer is not Wilkes.
3. Of the programmer and the person who bought the droid,
   one is Wilkes and the other is Hamming.
4. The writer is not Minsky.
5. Neither Knuth nor the person who bought the tablet is the manager.
6. Knuth arrived the day after Simon.
7. The person who arrived on Thursday is not the designer.
8. The person who arrived on Friday didn't buy the tablet.
9. The designer didn't buy the droid.
10. Knuth arrived the day after the manager.
11. Of the person who bought the laptop and Wilkes,
    one arrived on Monday and the other is the writer.
12. Either the person who bought the iphone or the person who bought the tablet
    arrived on Tuesday.

You will write the function logic_puzzle(), which should return a list of the
names of the people in the order in which they arrive. For example, if they
happen to arrive in alphabetical order, Hamming on Monday, Knuth on Tuesday, etc.,
then you would return:

['Hamming', 'Knuth', 'Minsky', 'Simon', 'Wilkes']

(You can assume that the days mentioned are all in the same week.)
"""


def logic_puzzle():
    "Return a list of the names of the people, in the order they arrive."
    ## your code here; you are free to define additional functions if needed
    


My solution:
"""
UNIT 2: Logic Puzzle

You will write code to solve the following logic puzzle:

1. The person who arrived on Wednesday bought the laptop.
2. The programmer is not Wilkes.
3. Of the programmer and the person who bought the droid,
   one is Wilkes and the other is Hamming.
4. The writer is not Minsky.
5. Neither Knuth nor the person who bought the tablet is the manager.
6. Knuth arrived the day after Simon.
7. The person who arrived on Thursday is not the designer.
8. The person who arrived on Friday didn't buy the tablet.
9. The designer didn't buy the droid.
10. Knuth arrived the day after the manager.
11. Of the person who bought the laptop and Wilkes,
    one arrived on Monday and the other is the writer.
12. Either the person who bought the iphone or the person who bought the tablet
    arrived on Tuesday.

You will write the function logic_puzzle(), which should return a list of the
names of the people in the order in which they arrive. For example, if they
happen to arrive in alphabetical order, Hamming on Monday, Knuth on Tuesday, etc.,
then you would return:

['Hamming', 'Knuth', 'Minsky', 'Simon', 'Wilkes']

(You can assume that the days mentioned are all in the same week.)
"""


def logic_puzzle():
    import itertools
    "Return a list of the names of the people, in the order they arrive."
    days = monday, tuesday, wednesday, thursday, friday = [1,2,3,4,5]
    orderings = list(itertools.permutations(days))
    (Wilkes, Hamming, Minsky, Knuth, Simon) = next((Wilkes, Hamming, Minsky, Knuth, Simon)
        for (Wilkes, Hamming, Minsky, Knuth, Simon) in orderings
            if Knuth == Simon + 1              # from [6]
        for (programmer, writer, manager, designer, unemployed) in orderings
            if programmer == Hamming           # from [2] and [3]
            and writer != Minsky               # from [4]
            and Knuth != manager               # from [5]
            and designer != thursday           # from [7]
            and Knuth == manager + 1           # from [10]
            and monday != writer               # from [11]
        for (laptop, droid, tablet, iphone, nothing) in orderings
            if laptop == wednesday             # from [1]
            and programmer != droid            # from [2] and [3]
            and droid == Wilkes                # from [2] and [3]
            and tablet != manager              # from [5]
            and tablet != friday               # from [8]
            and designer != droid              # from [9]
            and monday in [laptop, Wilkes]     # from [11]
            and writer in [laptop, Wilkes]     # from [11]
            and tuesday in [iphone, tablet]    # from [12]
    )
    solution = [
                (Wilkes, 'Wilkes'), 
                (Hamming, 'Hamming'), 
                (Minsky, 'Minsky'),
                (Knuth, 'Knuth'),
                (Simon, 'Simon')
               ]
    solution.sort()
    return [element[1] for element in solution]

print logic_puzzle()

No comments:

Post a Comment