示例#1
0
 def additional_answer(self, Houses: SuperSequence):
     (Nat1, Nat2) = n_Vars(2)
     for _ in members([
             House(nationality=Nat1, pet='zebra'),
             House(nationality=Nat2, drink='water')
     ], Houses):
         ans = f'\n\tThe {Nat1} both own a zebra and drink water.' if Nat1 == Nat2 else \
               f'\n\tThe {Nat1} own a zebra, and the {Nat2} drink water.'
         print(ans)
示例#2
0
def zebra_problem(Houses):
    for _ in forall([
            # 1. The English live in the red house.
            lambda: member(House(nationality='English', color='red'), Houses),
            # lambda: print_SF(f'After 1: {Houses}', 'Succeed'),

            # 2. The Spanish have a dog.
            lambda: member(House(nationality='Spanish', pet='dog'), Houses),
            # lambda: print_SF(f'After 2: {Houses}', 'Succeed'),

            # 3. They drink coffee in the green house.
            lambda: member(House(drink='coffee', color='green'), Houses),
            # lambda: print_SF(f'After 3: {Houses}', 'Succeed'),

            # 4. The Ukrainians drink tea.
            lambda: member(House(nationality='Ukrainians', drink='tea'), Houses
                           ),
            # lambda: print_SF(f'After 4: {Houses}', 'Succeed'),

            # 5. The green house is immediately to the right of the white house.
            lambda: is_contiguous_in(
                [House(color='white'),
                 House(color='green')], Houses),
            # lambda: print_SF(f'After 5: {Houses}', 'Succeed'),

            # 6. The Old Gold smokers have snails.
            lambda: member(House(smoke='Old Gold', pet='snails'), Houses),
            # lambda: print_SF(f'After 6: {Houses}', 'Succeed'),

            # 7. They smoke Kool in the yellow house.
            lambda: member(House(smoke='Kool', color='yellow'), Houses),
            # lambda: print_SF(f'After 7: {Houses}', 'Succeed'),

            # 8. They drink milk in the middle house.
            # Note the use of a slice. Houses[2] picks the middle house.
            lambda: unify(House(drink='milk'), Houses[2]),
            # lambda: print_SF(f'After 8: {Houses}', 'Succeed'),

            # 9. The Norwegians live in the first house on the left.
            lambda: unify(House(nationality='Norwegians'), Houses.head()),
            # lambda: print_SF(f'After 9: {Houses}', 'Succeed'),

            # 10. The Chesterfield smokers live next to the fox.
            lambda: next_to(House(smoke='Chesterfield'), House(pet='fox'),
                            Houses),
            # lambda: print_SF(f'After 10: {Houses}', 'Succeed'),

            # 11. They smoke Kool in the house next to the horse.
            lambda: next_to(House(smoke='Kool'), House(pet='horse'), Houses),
            # lambda: print_SF(f'After 11: {Houses}', 'Succeed'),

            # 12. The Lucky smokers drink juice.
            lambda: member(House(drink='juice', smoke='Lucky'), Houses),
            # lambda: print_SF(f'After 12: {Houses}', 'Succeed'),

            # 13. The Japanese smoke Parliament.
            lambda: member(House(nationality='Japanese', smoke='Parliament'),
                           Houses),
            # lambda: print_SF(f'After 13: {Houses}', 'Succeed'),

            # 14. The Norwegians live next to the blue house.
            lambda: next_to(House(nationality='Norwegians'), House(color='blue'
                                                                   ), Houses),
            # lambda: print_SF(f'After 14: {Houses}', 'Succeed'),

            # Fill in unmentioned properties.
            lambda: members([House(pet='zebra'),
                             House(drink='water')], Houses),
    ]):
        yield
示例#3
0
if __name__ == '__main__':

    from timeit import default_timer as timer

    (start1, end1, start2, end2) = (timer(), None, None, None)

    Houses = LinkedList([House() for _ in range(5)])
    inp = None
    for _ in zebra_problem(Houses):
        print('\nHouses: ')
        for (indx, house) in enumerate(Houses.to_python_list()):
            print(f'\t{indx+1}. {house}')
        (Nat1, Nat2) = n_Vars(2)
        for _ in members([
                House(nationality=Nat1, pet='zebra'),
                House(nationality=Nat2, drink='water')
        ], Houses):
            ans = f'The {Nat1} both own a zebra and drink water.' if Nat1 == Nat2 else \
                  f'The {Nat1} own a zebra, and the {Nat2} drink water.'
            end1 = timer()
            print(ans)
        inp = input('\nMore? (y, or n)? > ').lower()
        start2 = timer()
        if inp != 'y':
            break

    if inp == 'y':
        print('No more solutions.')

    end2 = timer()
示例#4
0
 def clue_15(self, Houses: SuperSequence):
     """ 15 (implicit) Fill in unmentioned properties. """
     yield from members([House(pet='zebra'), House(drink='water')], Houses)