From 3b19351ce44cdf4f0753024a7ac002070ecbadfe Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Fri, 22 Dec 2023 20:50:25 -0500 Subject: [PATCH] chessboard --- elixir/chessboard/.exercism/config.json | 21 ++++++ elixir/chessboard/.exercism/metadata.json | 1 + elixir/chessboard/.formatter.exs | 4 ++ elixir/chessboard/.gitignore | 24 +++++++ elixir/chessboard/HELP.md | 75 ++++++++++++++++++++++ elixir/chessboard/HINTS.md | 32 +++++++++ elixir/chessboard/README.md | 71 ++++++++++++++++++++ elixir/chessboard/lib/chessboard.ex | 6 ++ elixir/chessboard/mix.exs | 28 ++++++++ elixir/chessboard/test/chessboard_test.exs | 23 +++++++ elixir/chessboard/test/test_helper.exs | 2 + 11 files changed, 287 insertions(+) create mode 100644 elixir/chessboard/.exercism/config.json create mode 100644 elixir/chessboard/.exercism/metadata.json create mode 100644 elixir/chessboard/.formatter.exs create mode 100644 elixir/chessboard/.gitignore create mode 100644 elixir/chessboard/HELP.md create mode 100644 elixir/chessboard/HINTS.md create mode 100644 elixir/chessboard/README.md create mode 100644 elixir/chessboard/lib/chessboard.ex create mode 100644 elixir/chessboard/mix.exs create mode 100644 elixir/chessboard/test/chessboard_test.exs create mode 100644 elixir/chessboard/test/test_helper.exs diff --git a/elixir/chessboard/.exercism/config.json b/elixir/chessboard/.exercism/config.json new file mode 100644 index 0000000..75e1bae --- /dev/null +++ b/elixir/chessboard/.exercism/config.json @@ -0,0 +1,21 @@ +{ + "authors": [ + "angelikatyborska" + ], + "contributors": [ + "neenjaw" + ], + "files": { + "solution": [ + "lib/chessboard.ex" + ], + "test": [ + "test/chessboard_test.exs" + ], + "exemplar": [ + ".meta/exemplar.ex" + ] + }, + "language_versions": ">=1.10", + "blurb": "Learn about ranges by generating a chessboard." +} diff --git a/elixir/chessboard/.exercism/metadata.json b/elixir/chessboard/.exercism/metadata.json new file mode 100644 index 0000000..c76ee52 --- /dev/null +++ b/elixir/chessboard/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"chessboard","id":"639c84f70f71467b9140bcfcb8a192d4","url":"https://exercism.org/tracks/elixir/exercises/chessboard","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/chessboard/.formatter.exs b/elixir/chessboard/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/chessboard/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/chessboard/.gitignore b/elixir/chessboard/.gitignore new file mode 100644 index 0000000..4abee35 --- /dev/null +++ b/elixir/chessboard/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +match_binary-*.tar + diff --git a/elixir/chessboard/HELP.md b/elixir/chessboard/HELP.md new file mode 100644 index 0000000..4454cd3 --- /dev/null +++ b/elixir/chessboard/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/chessboard.ex` 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 [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [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. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/chessboard/HINTS.md b/elixir/chessboard/HINTS.md new file mode 100644 index 0000000..dfc3667 --- /dev/null +++ b/elixir/chessboard/HINTS.md @@ -0,0 +1,32 @@ +# Hints + +## General + +- Read the official documentation for [ranges][range]. + +## 1. Define the rank range + +- There is a [special operator][range-creation-operator] for creating ranges. + +## 2. Define the file range + +- There is a [special operator][range-creation-operator] for creating ranges. +- There is a [special syntax][unicode-code-points] to write a character code point without explicitly knowing its value. + +## 3. Transform the rank range into a list of ranks + +- Ranges implement the `Enumerable` protocol. +- There is a [built-in function][enum-to-list] to change an enumerable data structure to a list. + +## 4. Transform the file range into a list of files + +- Ranges implement the `Enumerable` protocol. +- There is a [built-in function][enum-map] to change an enumerable data structure to a list while modifying its elements. +- The [bitstring special form][bitstring-special-form] can be used to turn a code point into a string. + +[range]: https://hexdocs.pm/elixir/Range.html +[range-creation-operator]: https://hexdocs.pm/elixir/Kernel.html#../2 +[unicode-code-points]: https://hexdocs.pm/elixir/syntax-reference.html#integers-in-other-bases-and-unicode-code-points +[enum-to-list]: https://hexdocs.pm/elixir/Enum.html#to_list/1 +[enum-map]: https://hexdocs.pm/elixir/Enum.html#map/2 +[bitstring-special-form]: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#%3C%3C%3E%3E/1 \ No newline at end of file diff --git a/elixir/chessboard/README.md b/elixir/chessboard/README.md new file mode 100644 index 0000000..71c64a3 --- /dev/null +++ b/elixir/chessboard/README.md @@ -0,0 +1,71 @@ +# Chessboard + +Welcome to Chessboard on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Ranges + +Ranges represent a sequence of one or many consecutive integers. They are created by connecting two integers with `..`. + +```elixir +1..5 +``` + +Ranges can be ascending or descending. They are always inclusive of the first and last values. + +A range implements the _Enumerable protocol_, which means functions in the `Enum` module can be used to work with ranges. + +## Instructions + +As a chess enthusiast, you would like to write your own version of the game. Yes, there maybe plenty of implementations of chess available online already, but yours will be unique! + +But before you can let your imagination run wild, you need to take care of the basics. Let's start by generating the board. + +Each square of the chessboard is identified by a letter-number pair. The vertical columns of squares, called files, are labeled A through H. The horizontal rows of squares, called ranks, are numbered 1 to 8. + +## 1. Define the rank range + +Implement the `rank_range/0` function. It should return a range of integers, from 1 to 8. + +```elixir +Chessboard.rank_range() +``` + +## 2. Define the file range + +Implement the `file_range/0` function. It should return a range of integers, from the code point of the uppercase letter A, to the code point of the uppercase letter H. + +```elixir +Chessboard.file_range() +``` + +## 3. Transform the rank range into a list of ranks + +Implement the `ranks/0` function. It should return a list of integers, from 1 to 8. Do not write the list by hand, generate it from the range returned by the `rank_range/0` function. + +```elixir +Chessboard.ranks() +# => [1, 2, 3, 4, 5, 6, 7, 8] +``` + +## 4. Transform the file range into a list of files + +Implement the `files/0` function. It should return a list of letters (strings), from "A" to "H". Do not write the list by hand, generate it from the range returned by the `file_range/0` function. + +```elixir +Chessboard.files() +# => ["A", "B", "C", "D", "E", "F", "G", "H"] +``` + +## Source + +### Created by + +- @angelikatyborska + +### Contributed to by + +- @neenjaw \ No newline at end of file diff --git a/elixir/chessboard/lib/chessboard.ex b/elixir/chessboard/lib/chessboard.ex new file mode 100644 index 0000000..da5c6e0 --- /dev/null +++ b/elixir/chessboard/lib/chessboard.ex @@ -0,0 +1,6 @@ +defmodule Chessboard do + def rank_range, do: 1..8 + def file_range, do: ?A..?H + def ranks, do: rank_range() |> Enum.to_list + def files, do: file_range() |> Enum.map(&(<<&1>>)) +end diff --git a/elixir/chessboard/mix.exs b/elixir/chessboard/mix.exs new file mode 100644 index 0000000..e790271 --- /dev/null +++ b/elixir/chessboard/mix.exs @@ -0,0 +1,28 @@ +defmodule Chessboard.MixProject do + use Mix.Project + + def project do + [ + app: :chessboard, + version: "0.1.0", + # elixir: "~> 1.10", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/chessboard/test/chessboard_test.exs b/elixir/chessboard/test/chessboard_test.exs new file mode 100644 index 0000000..22e8fa4 --- /dev/null +++ b/elixir/chessboard/test/chessboard_test.exs @@ -0,0 +1,23 @@ +defmodule ChessboardTest do + use ExUnit.Case + + @tag task_id: 1 + test "rank_range is a range from 1 to 8" do + assert Chessboard.rank_range() == 1..8 + end + + @tag task_id: 2 + test "file_range is a range from ?A to ?H" do + assert Chessboard.file_range() == ?A..?H + end + + @tag task_id: 3 + test "ranks is a list of integers from 1 to 8" do + assert Chessboard.ranks() == [1, 2, 3, 4, 5, 6, 7, 8] + end + + @tag task_id: 4 + test "files is a list of letters (strings) from A to H" do + assert Chessboard.files() == ["A", "B", "C", "D", "E", "F", "G", "H"] + end +end diff --git a/elixir/chessboard/test/test_helper.exs b/elixir/chessboard/test/test_helper.exs new file mode 100644 index 0000000..e8677a3 --- /dev/null +++ b/elixir/chessboard/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true, seed: 0)