Skip to content

Commit

Permalink
Add wedding-woes exercise (#438)
Browse files Browse the repository at this point in the history
* wedding-woes: add exercise

* wedding-woes: add exercise
  • Loading branch information
ErikSchierboom authored Sep 10, 2024
1 parent ce3bac5 commit c9fd901
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "wedding-woes",
"name": "wedding-woes",
"uuid": "e11fad7c-c21f-49af-a607-9df50ef5fa41",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "queen-attack",
"name": "Queen Attack",
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/wedding-woes/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Hints

## General

- The [Prolog basics page](https://www.tutorialspoint.com/prolog/prolog_basics.htm) explains what facts and rules are and how to define them

## 1. List the chatty people

- You're defining a [fact](https://www.tutorialspoint.com/prolog/prolog_basics.htm)
- You can define multiple "instances" of the same fact
- Remember to use lowercase names

## 2. List who likes whom

- You're defining a [fact](https://www.tutorialspoint.com/prolog/prolog_basics.htm)
- You can define multiple "instances" of the same fact
- Remember to use lowercase names
- The first argument is the person liking the other person
- The second argument is the person being liked by the other person

## 3. People who like each other make a good pairing

- You're defining a [rule](https://www.tutorialspoint.com/prolog/prolog_basics.htm)
- In Prolog, making sure that two facts/rules are both true is done via a [conjunction](https://www.tutorialspoint.com/prolog/prolog_conjunctions_and_disjunctions.htm)

## 4. Chatty people pair with anyone

- You're defining a [rule](https://www.tutorialspoint.com/prolog/prolog_basics.htm)
- In Prolog, making sure that at least one of two facts/rules is true is done via a [disjunction](https://www.tutorialspoint.com/prolog/prolog_conjunctions_and_disjunctions.htm)

## 5. Check seating arrangement

- You're defining a [rule](https://www.tutorialspoint.com/prolog/prolog_basics.htm)
68 changes: 68 additions & 0 deletions exercises/practice/wedding-woes/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Instructions

Your task is to find the perfect table seating.
You'll do this by declaring _facts_ (things that are always true) and _rules_ (things that are conditionally true).

## 1. List the chatty people

Define the `chatty` fact to represent the chatty people.
The `chatty` fact takes a single argument: the chatty person's lowercase name.

```prolog
chatty(gustavo).
true.
```

## 2. List who likes whom

Define the `likes` fact to represent who likes whom.
The `likes` fact takes two arguments:

1. The lowercase name of the person liking the other person
2. The lowercase name of the person being liked

```prolog
likes(esteban, malena).
true.
```

## 3. People who like each other make a good pairing

Define the `pairing` rule to be true when the two people both like each other.
The pairing rule takes two arguments:

1. The lowercase name of the first person
2. The lowercase name of the second person

```prolog
pairing(malena, esteban).
true.
```

## 4. Chatty people pair with anyone

Define the `pairing` rule to be true when (at least) one of the two people being paired is chatty.

```prolog
pairing(valeria, jaime).
true.
```

## 5. Check seating arrangement

Define the `seating` rule to be true when all five pairs are good pairings.

The pairing rule takes fives arguments:

1. The lowercase name of the first person
2. The lowercase name of the second person
3. The lowercase name of the third person
4. The lowercase name of the fourth person
5. The lowercase name of the fifth person

Use the just defined facts and rules to check the seating arrangement.

```prolog
seating(esteban, jaime, gustavo, malena, valeria).
false.
```
24 changes: 24 additions & 0 deletions exercises/practice/wedding-woes/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Introduction

Your best friend is getting married and asked you to be their wedding planner.
You agreed, though everything you know about it is from a movie...
Things were going smoothly, until you started working on the table seating.

Five guests will be attending, seated around one big, round table.
Here's the guest list:

- Esteban, Gustavo, Jaime, Malena, and Valeria.

Your goal is to find a seating arrangement where everyone is happy with their neighbors.
You don't know everyone quite as well as you'd like, but here's what you do know:

- Gustavo is chatty
- Valeria is chatty
- Esteban likes Malena
- Malena likes Esteban
- Rico likes Naran (but not vice versa!)

People who like each other can be seated next to each other, but only when they both feel that way.
Chatty people intermingle easily and can be seated next to anyone.

With these details in hand, it's time to sketch out a seating arrangement!
17 changes: 17 additions & 0 deletions exercises/practice/wedding-woes/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"wedding_woes.pl"
],
"test": [
"wedding_woes_tests.plt"
],
"example": [
".meta/wedding_woes.example.pl"
]
},
"blurb": "Infer family relations from some basic facts."
}
10 changes: 10 additions & 0 deletions exercises/practice/wedding-woes/.meta/wedding_woes.example.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
chatty(gustavo).
chatty(valeria).

likes(esteban, malena).
likes(malena, esteban).
likes(gustavo, valeria).

pairing(First, Second) :- likes(First, Second), likes(Second, First).
pairing(First, Second) :- chatty(First); chatty(Second).
seating(A, B, C, D, E) :- pairing(A, B), pairing(B, C), pairing(C, D), pairing(D, E), pairing(E, A).
7 changes: 7 additions & 0 deletions exercises/practice/wedding-woes/wedding_woes.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
% Define the 'chatty' fact

% Define the 'lines' fact

% Define the 'pairing' rule

% Define the 'seating' rule
48 changes: 48 additions & 0 deletions exercises/practice/wedding-woes/wedding_woes_tests.plt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.

:- begin_tests(wedding_woes).

test(gustavo_is_chatty, condition(true)) :-
chatty(gustavo).

test(valeria_is_chatty, condition(pending)) :-
chatty(valeria).

test(jaime_is_not_chatty, [fail, condition(pending)]) :-
chatty(jaime).

test(esteban_likes_malena, condition(pending)) :-
likes(esteban, malena).

test(malena_likes_esteban, condition(pending)) :-
likes(malena, esteban).

test(gustavo_likes_valeria, condition(pending)) :-
likes(gustavo, valeria).

test(valeria_does_not_like_gustavo, [fail, condition(pending)]) :-
likes(valeria, gustavo).

test(people_who_like_each_other_make_a_good_pairing, condition(pending)) :-
pairing(malena, esteban).

test(two_chatty_people_make_a_good_pairing, condition(pending)) :-
pairing(gustavo, jaime).

test(one_chatty_person_make_a_good_pairing_with_anyone, condition(pending)) :-
pairing(valeria, jaime).

test(people_who_dont_like_each_other_and_are_not_chatty_dont_make_a_good_pairing, [fail, condition(pending)]) :-
pairing(rico, naran).

test(valid_seating, condition(pending)) :-
seating(esteban, malena, gustavo, jaime, valeria).

test(invalid_seating, [fail, condition(pending)]) :-
seating(esteban, jaime, gustavo, malena, valeria).

:- end_tests(wedding_woes).

0 comments on commit c9fd901

Please sign in to comment.