1
1
Fork 0

feat(prolog): queen attack, triangle

Signed-off-by: Christina Sørensen <ces@fem.gg>
This commit is contained in:
Christina Sørensen 2024-12-23 17:33:51 +01:00
parent 9ffc70cd57
commit c0c5c65f9f
Signed by: cafkafk
GPG key ID: F67767BE4545A600
12 changed files with 475 additions and 0 deletions

View file

@ -0,0 +1,22 @@
{
"authors": [
"Average-user"
],
"contributors": [
"jackhughesweb"
],
"files": {
"solution": [
"queen_attack.pl"
],
"test": [
"queen_attack_tests.plt"
],
"example": [
".meta/queen_attack.example.pl"
]
},
"blurb": "Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.",
"source": "J Dalbey's Programming Practice problems",
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
}

View file

@ -0,0 +1 @@
{"track":"prolog","exercise":"queen-attack","id":"a59aa03a8fcf4a8e8e3d38148fda0d2e","url":"https://exercism.org/tracks/prolog/exercises/queen-attack","handle":"cafkafk","is_requester":true,"auto_approve":false}

View file

@ -0,0 +1,84 @@
# Help
## Running the tests
## Command line
The following command can be used to run the tests from the command line:
```bash
swipl -f <exercise>.pl -s <exercise>_tests.plt -g run_tests,halt -t 'halt(1)'
```
Replace `<exercise>` with the name of the exercise you are implementing.
## Interactive
To run prolog interactively first run:
```bash
swipl
```
After the prolog console starts, load your implementation and run the tests
with:
```
?- ["<exercise>.pl"].
?- ["<exercise>_tests.plt"].
?- run_tests.
```
Replace `<exercise>` with the name of the exercise you are implementing.
### Reloading changes
Once the above files are loaded, you can apply any changes you've made
by running:
```
?- make.
```
## Skipped tests
When you first begin an exercise, only the first test will run. The rest have
been skipped by adding `condition(pending)` to the `test` goal. Once the first
test passes, un-skip the next test by changing `pending` in `condition(pending)`
to `true`. Repeat for each test until they are all running and passing.
### Command line
Add the `-- --all` argument to the end of the command to also run any pending tests:
```bash
swipl -f <exercise>.pl -s <exercise>_tests.plt -g run_tests,halt -t 'halt(1)' -- --all
```
## Submitting your solution
You can submit your solution using the `exercism submit queen_attack.pl` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Prolog track's documentation](https://exercism.org/docs/tracks/prolog)
- The [Prolog track's programming category on the forum](https://forum.exercism.org/c/programming/prolog)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [SWI Prolog Documentation](http://www.swi-prolog.org)
- [Tutorials and Resources](http://www.swi-prolog.org/Links.html)
- [/r/prolog](https://www.reddit.com/r/prolog) is the Prolog subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/prolog) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

View file

@ -0,0 +1,40 @@
# Queen Attack
Welcome to Queen Attack on Exercism's Prolog Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.
In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.
A chessboard can be represented by an 8 by 8 array.
So if you are told the white queen is at `c5` (zero-indexed at column 2, row 3) and the black queen at `f2` (zero-indexed at column 5, row 6), then you know that the set-up is like so:
![A chess board with two queens. Arrows emanating from the queen at c5 indicate possible directions of capture along file, rank and diagonal.](https://assets.exercism.org/images/exercises/queen-attack/queen-capture.svg)
You are also able to answer whether the queens can attack each other.
In this case, that answer would be yes, they can, because both pieces share a diagonal.
## Credit
The chessboard image was made by [habere-et-dispertire][habere-et-dispertire] using LaTeX and the [chessboard package][chessboard-package] by Ulrike Fischer.
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
[chessboard-package]: https://github.com/u-fischer/chessboard
## Source
### Created by
- @Average-user
### Contributed to by
- @jackhughesweb
### Based on
J Dalbey's Programming Practice problems - https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

View file

@ -0,0 +1,16 @@
%! create(+DimTuple)
%
% The create/1 predicate succeeds if the DimTuple contains valid chessboard
% dimensions, e.g. (0,0) or (2,4).
create((X, Y)) :-
between(0,7,X),
between(0,7,Y).
%! attack(+FromTuple, +ToTuple)
%
% The attack/2 predicate succeeds if a queen positioned on ToTuple is
% vulnerable to an attack by another queen positioned on FromTuple.
attack((FromX, FromY), (ToX, ToY)):-
FromX =:= ToX;
FromY =:= ToY;
abs(FromX - ToX) =:= abs(FromY - ToY).

View file

@ -0,0 +1,53 @@
pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.
:- begin_tests(create_tests).
test(create_in_center, condition(true)) :-
create((3,3)).
test(valid_position, condition(pending)) :-
create((2,2)).
test(must_have_positive_row, [fail, condition(pending)]) :-
create((-2,2)).
test(row_smaller_than_board_size, [fail, condition(pending)]) :-
create((8,4)).
test(must_have_positive_column, [fail, condition(pending)]) :-
create((2,-2)).
test(column_smaller_than_board_size, [fail, condition(pending)]) :-
create((4,8)).
:- end_tests(create_tests).
:- begin_tests(attack_tests).
test(cant_attack, [fail, condition(pending)]) :-
attack((2,4), (6,6)).
test(attack_on_same_row, condition(pending)) :-
attack((2,4), (2,6)).
test(attack_same_column, condition(pending)) :-
attack((4,5), (2,5)).
test(attack_first_diagonal, condition(pending)) :-
attack((2,2), (0,4)).
test(attack_second_diagonal, condition(pending)) :-
attack((2,2), (3,1)).
test(attack_third_diagonal, condition(pending)) :-
attack((2,2), (1,1)).
test(attack_fourth_diagonal, condition(pending)) :-
attack((2,2), (5,5)).
:- end_tests(attack_tests).

View file

@ -0,0 +1,22 @@
{
"authors": [
"qjd2413"
],
"contributors": [
"parkerl"
],
"files": {
"solution": [
"triangle.pl"
],
"test": [
"triangle_tests.plt"
],
"example": [
".meta/triangle.example.pl"
]
},
"blurb": "Determine if a triangle is equilateral, isosceles, or scalene.",
"source": "The Ruby Koans triangle project, parts 1 & 2",
"source_url": "https://web.archive.org/web/20220831105330/http://rubykoans.com"
}

View file

@ -0,0 +1 @@
{"track":"prolog","exercise":"triangle","id":"21717ff9a0184d388e63a4756ada73ea","url":"https://exercism.org/tracks/prolog/exercises/triangle","handle":"cafkafk","is_requester":true,"auto_approve":false}

84
prolog/triangle/HELP.md Normal file
View file

@ -0,0 +1,84 @@
# Help
## Running the tests
## Command line
The following command can be used to run the tests from the command line:
```bash
swipl -f <exercise>.pl -s <exercise>_tests.plt -g run_tests,halt -t 'halt(1)'
```
Replace `<exercise>` with the name of the exercise you are implementing.
## Interactive
To run prolog interactively first run:
```bash
swipl
```
After the prolog console starts, load your implementation and run the tests
with:
```
?- ["<exercise>.pl"].
?- ["<exercise>_tests.plt"].
?- run_tests.
```
Replace `<exercise>` with the name of the exercise you are implementing.
### Reloading changes
Once the above files are loaded, you can apply any changes you've made
by running:
```
?- make.
```
## Skipped tests
When you first begin an exercise, only the first test will run. The rest have
been skipped by adding `condition(pending)` to the `test` goal. Once the first
test passes, un-skip the next test by changing `pending` in `condition(pending)`
to `true`. Repeat for each test until they are all running and passing.
### Command line
Add the `-- --all` argument to the end of the command to also run any pending tests:
```bash
swipl -f <exercise>.pl -s <exercise>_tests.plt -g run_tests,halt -t 'halt(1)' -- --all
```
## Submitting your solution
You can submit your solution using the `exercism submit triangle.pl` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Prolog track's documentation](https://exercism.org/docs/tracks/prolog)
- The [Prolog track's programming category on the forum](https://forum.exercism.org/c/programming/prolog)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [SWI Prolog Documentation](http://www.swi-prolog.org)
- [Tutorials and Resources](http://www.swi-prolog.org/Links.html)
- [/r/prolog](https://www.reddit.com/r/prolog) is the Prolog subreddit.
- [StackOverflow](http://stackoverflow.com/questions/tagged/prolog) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

53
prolog/triangle/README.md Normal file
View file

@ -0,0 +1,53 @@
# Triangle
Welcome to Triangle on Exercism's Prolog Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Determine if a triangle is equilateral, isosceles, or scalene.
An _equilateral_ triangle has all three sides the same length.
An _isosceles_ triangle has at least two sides the same length.
(It is sometimes specified as having exactly two sides the same length, but for the purposes of this exercise we'll say at least two.)
A _scalene_ triangle has all sides of different lengths.
## Note
For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side.
In equations:
Let `a`, `b`, and `c` be sides of the triangle.
Then all three of the following expressions must be true:
```text
a + b ≥ c
b + c ≥ a
a + c ≥ b
```
See [Triangle Inequality][triangle-inequality]
[triangle-inequality]: https://en.wikipedia.org/wiki/Triangle_inequality
## Bonus: mixed types
If you're up for it, there are a couple of bonus tests where the types of the arguments are not all the same.
These tests are optional, so you don't _have_ to solve before submitting your solution.
## Source
### Created by
- @qjd2413
### Contributed to by
- @parkerl
### Based on
The Ruby Koans triangle project, parts 1 & 2 - https://web.archive.org/web/20220831105330/http://rubykoans.com

View file

@ -0,0 +1,7 @@
triangle(A, B, C, T) :-
A + B + C > 0,
(
A + B >= C,
B + C >= A,
A + C >= B
).

View file

@ -0,0 +1,92 @@
pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.
:- begin_tests(equilateral_triangle).
test(all_sides_are_equal, condition(true)) :-
triangle(2, 2, 2, "equilateral").
test(any_side_is_unequal, [fail, condition(pending)]) :-
triangle(2, 3, 2, "equilateral").
test(no_sides_are_equal, [fail, condition(pending)]) :-
triangle(5, 4, 6, "equilateral").
test(all_zero_sides_are_not_a_triangle, [fail, condition(pending)]) :-
triangle(0, 0, 0, "equilateral").
test(all_sides_are_floats_and_equal, condition(pending)) :-
triangle((0.5), (0.5), (0.5), "equilateral").
:- end_tests(equilateral_triangle).
:- begin_tests(isosceles_triangle).
test(last_two_sides_equal, condition(pending)) :-
triangle(3, 4, 4, "isosceles").
test(first_two_sides_equal, condition(pending)) :-
triangle(4, 4, 3, "isosceles").
test(first_and_last_sides_equal, condition(pending)) :-
triangle(4, 3, 4, "isosceles").
test(equilateral_triangles_are_also_isosceles, condition(pending)) :-
triangle(4, 4, 4, "isosceles").
test(no_sides_are_equal, [fail, condition(pending)]) :-
triangle(2, 3, 4, "isosceles").
test(first_triangle_inequality_violation, [fail, condition(pending)]) :-
triangle(1, 1, 3, "isosceles").
test(second_triangle_inequality_violation, [fail, condition(pending)]) :-
triangle(1, 3, 1, "isosceles").
test(third_triangle_inequality_violation, [fail, condition(pending)]) :-
triangle(3, 1, 1, "isosceles").
test(sides_may_be_floats, condition(pending)) :-
triangle((0.5), (0.4), (0.5), "isosceles").
:- end_tests(isosceles_triangle).
:- begin_tests(scalene_triangle).
test(no_sides_are_equal, condition(pending)) :-
triangle(5, 4, 6, "scalene").
test(all_sides_are_equal, [fail, condition(pending)]) :-
triangle(4, 4, 4, "scalene").
test(two_sides_are_equal, [fail, condition(pending)]) :-
triangle(4, 4, 3, "scalene").
test(may_not_violate_triangle_inequality, [fail, condition(pending)]) :-
triangle(7, 3, 2, "scalene").
test(small_scalene_triangle_with_floating_point_values, condition(pending)) :-
triangle((0.5), (0.4), (0.6), "scalene").
:- end_tests(scalene_triangle).
% BONUS TESTS
bonus :-
current_prolog_flag(argv, ['--bonus'|_]).
:- begin_tests(triangle_bonus, [condition(bonus)]).
test(all_sides_are_equal_but_mixed_types) :-
triangle(2, 2.0, 2, "equilateral").
test(sides_are_a_mix) :-
triangle(5, 4, 5.0, "isosceles").
test(no_sides_are_equal_mixed) :-
triangle(5.0, 4, 6, "scalene").
:- end_tests(triangle_bonus).