0

`

import itertools
def imright(h1, h2):
    "House h1 is immediately right of h2 if h1-h2 == 1."
    return h1-h2 == 1

def nextto(h1, h2):
    "Two houses are next to each other if they differ by 1."
    return abs(h1-h2) == 1
def find_water_zebra():
    houses = [first,_,middle,_,_] = [1,2,3,4,5]
    orderings = list(itertools.permutations(houses))
    return [result for result in (
            (   ('Drinks',  {'coffee':coffee,'tea':tea,'milk':milk,'WATER':WATER,'oj':oj}),
                ('Nations', {'Englishman':Englishman, 'Spaniard':Spaniard,
                               'Ukranian':Ukranian, 'Japanese':Japanese, 'Norwegian':Norwegian}),
                ('Colours', {'red':red, 'green':green, 'ivory':ivory, 'yellow':yellow, 'blue':blue}),
                ('Pets',    {'dog':dog, 'snails':snails, 'fox':fox, 'horse':horse, 'ZEBRA':ZEBRA}),
                ('Smokes',  {'OldGold':OldGold, 'Kools':Kools, 'Chesterfields':Chesterfields,
                                'LuckyStrike':LuckyStrike, 'Parliaments':Parliaments}),
            )
        for(red, green, ivory, yellow, blue) in orderings
        if imright(green, ivory)        #6
        for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings
        if Englishman is red           #2
        if Norwegian is first           #10
        if nextto(Norwegian, blue)      #15
        for (coffee, tea, milk, oj, WATER) in orderings
        if coffee is green               #4
        if Ukranian is tea              #5
        if milk is middle               #9
        for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in orderings
        if Kools is yellow              #8
        if LuckyStrike is oj            #13
        if Japanese is Parliaments      #14
        for (dog, snails, fox, horse, ZEBRA) in orderings
        if Spaniard is dog              #3
        if OldGold is snails            #7
        if nextto(Chesterfields, fox)
        if nextto(Kools, horse)
    )
 ]




print (find_water_zebra())

`

I want this code to give me "Norwegian lives in the yellow house 1, drinks water, smokes Kools and has a fox"

but I can't seem to sort things accordingly. So it gives me the answers I want but very ugly. [(('Drinks', {'coffee': 5, 'tea': 2, 'milk': 3, 'WATER': 1, 'oj': 4}), ('Nations', {'Englishman': 3, 'Spaniard': 4, 'Ukranian': 2, 'Japanese': 5, 'Norwegian': 1}), ('Colours', {'red': 3, 'green': 5, 'ivory': 4, 'yellow': 1, 'blue': 2}), ('Pets', {'dog': 4, 'snails': 3, 'fox': 1, 'horse': 2, 'ZEBRA': 5}), ('Smokes', {'OldGold': 3, 'Kools': 1, 'Chesterfields': 2, 'LuckyStrike': 4, 'Parliaments': 5}))]

How can I sort this according to houses?

Sorry I'm struggling quite a lot as an electrical engineering student taking a computer engineering class...thank you for the help :)

1 Answers1

0

Instead of using tuples, I edited your output such that is a dictionary, which makes it easier to get the right words as output. I created a function to get the right output from this dictionary:

def print_output(output, house):

    key_words = ['Nations', 'Colours', 'Drinks', 'Smokes', 'Pets']
    val_words = []
    for key in key_words:
        val_words.append(list(output[key].keys())[list(output[key].values()).index(house)])
    nation, colour, drink, smoke, pet = val_words

    return f"{nation} lives in the {colour} house {house}, drinks {drink}, smokes {smoke} and has a {pet}"

For each key_word I find the corresponding value for the right house number. Then, with f-strings I create the desired output.

When using the updated find_water_zebra() function, I get the right output.

def find_water_zebra():
    houses = [first,_,middle,_,_] = [1,2,3,4,5]
    orderings = list(itertools.permutations(houses))
    output = [result for result in (
           (     {'Drinks':  {'coffee':coffee,'tea':tea,'milk':milk,'WATER':WATER,'oj':oj},
                'Nations': {'Englishman':Englishman, 'Spaniard':Spaniard,
                            'Ukranian':Ukranian, 'Japanese':Japanese, 'Norwegian':Norwegian},
                'Colours': {'red':red, 'green':green, 'ivory':ivory, 'yellow':yellow, 'blue':blue},
                'Pets':    {'dog':dog, 'snails':snails, 'fox':fox, 'horse':horse, 'ZEBRA':ZEBRA},
                'Smokes':  {'OldGold':OldGold, 'Kools':Kools, 'Chesterfields':Chesterfields,
                                'LuckyStrike':LuckyStrike, 'Parliaments':Parliaments}}
            )
        for(red, green, ivory, yellow, blue) in orderings
        if imright(green, ivory)        #6
        for (Englishman, Spaniard, Ukranian, Japanese, Norwegian) in orderings
        if Englishman is red           #2
        if Norwegian is first           #10
        if nextto(Norwegian, blue)      #15
        for (coffee, tea, milk, oj, WATER) in orderings
        if coffee is green               #4
        if Ukranian is tea              #5
        if milk is middle               #9
        for (OldGold, Kools, Chesterfields, LuckyStrike, Parliaments) in orderings
        if Kools is yellow              #8
        if LuckyStrike is oj            #13
        if Japanese is Parliaments      #14
        for (dog, snails, fox, horse, ZEBRA) in orderings
        if Spaniard is dog              #3
        if OldGold is snails            #7
        if nextto(Chesterfields, fox)
        if nextto(Kools, horse)
        )
        ]
    return print_output(output[0], 1)
T C Molenaar
  • 3,205
  • 1
  • 10
  • 26