From 8fbd81ed9dda1cd791073b1cb8f94b38a82de8f7 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 29 Jun 2024 02:40:08 -0400 Subject: [PATCH] scrabble-score --- elixir/scrabble-score/.exercism/config.json | 35 +++++++++ elixir/scrabble-score/.exercism/metadata.json | 1 + elixir/scrabble-score/.formatter.exs | 4 + elixir/scrabble-score/.gitignore | 24 ++++++ elixir/scrabble-score/HELP.md | 75 +++++++++++++++++++ elixir/scrabble-score/README.md | 65 ++++++++++++++++ elixir/scrabble-score/lib/scrabble.ex | 21 ++++++ elixir/scrabble-score/mix.exs | 28 +++++++ elixir/scrabble-score/test/scrabble_test.exs | 47 ++++++++++++ elixir/scrabble-score/test/test_helper.exs | 2 + 10 files changed, 302 insertions(+) create mode 100644 elixir/scrabble-score/.exercism/config.json create mode 100644 elixir/scrabble-score/.exercism/metadata.json create mode 100644 elixir/scrabble-score/.formatter.exs create mode 100644 elixir/scrabble-score/.gitignore create mode 100644 elixir/scrabble-score/HELP.md create mode 100644 elixir/scrabble-score/README.md create mode 100644 elixir/scrabble-score/lib/scrabble.ex create mode 100644 elixir/scrabble-score/mix.exs create mode 100644 elixir/scrabble-score/test/scrabble_test.exs create mode 100644 elixir/scrabble-score/test/test_helper.exs diff --git a/elixir/scrabble-score/.exercism/config.json b/elixir/scrabble-score/.exercism/config.json new file mode 100644 index 0000000..33f3304 --- /dev/null +++ b/elixir/scrabble-score/.exercism/config.json @@ -0,0 +1,35 @@ +{ + "authors": [ + "rubysolo" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "devonestes", + "etrepum", + "jinyeow", + "kytrinyx", + "lpil", + "neenjaw", + "parkerl", + "sotojuan", + "Teapane", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/scrabble.ex" + ], + "test": [ + "test/scrabble_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a word, compute the Scrabble score for that word.", + "source": "Inspired by the Extreme Startup game", + "source_url": "https://github.com/rchatley/extreme_startup" +} diff --git a/elixir/scrabble-score/.exercism/metadata.json b/elixir/scrabble-score/.exercism/metadata.json new file mode 100644 index 0000000..9f676ed --- /dev/null +++ b/elixir/scrabble-score/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"scrabble-score","id":"7f7eb3dce06b404b9c288b6ebbe97a40","url":"https://exercism.org/tracks/elixir/exercises/scrabble-score","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/scrabble-score/.formatter.exs b/elixir/scrabble-score/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/scrabble-score/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/scrabble-score/.gitignore b/elixir/scrabble-score/.gitignore new file mode 100644 index 0000000..7ef5a81 --- /dev/null +++ b/elixir/scrabble-score/.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"). +scrabble_score-*.tar + diff --git a/elixir/scrabble-score/HELP.md b/elixir/scrabble-score/HELP.md new file mode 100644 index 0000000..003d2b9 --- /dev/null +++ b/elixir/scrabble-score/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/scrabble.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/scrabble-score/README.md b/elixir/scrabble-score/README.md new file mode 100644 index 0000000..6263d2a --- /dev/null +++ b/elixir/scrabble-score/README.md @@ -0,0 +1,65 @@ +# Scrabble Score + +Welcome to Scrabble Score on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +[Scrabble][wikipedia] is a word game where players place letter tiles on a board to form words. +Each letter has a value. +A word's score is the sum of its letters' values. + +[wikipedia]: https://en.wikipedia.org/wiki/Scrabble + +## Instructions + +Your task is to compute a word's Scrabble score by summing the values of its letters. + +The letters are valued as follows: + +| Letter | Value | +| ---------------------------- | ----- | +| A, E, I, O, U, L, N, R, S, T | 1 | +| D, G | 2 | +| B, C, M, P | 3 | +| F, H, V, W, Y | 4 | +| K | 5 | +| J, X | 8 | +| Q, Z | 10 | + +For example, the word "cabbage" is worth 14 points: + +- 3 points for C +- 1 point for A +- 3 points for B +- 3 points for B +- 1 point for A +- 2 points for G +- 1 point for E + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @devonestes +- @etrepum +- @jinyeow +- @kytrinyx +- @lpil +- @neenjaw +- @parkerl +- @sotojuan +- @Teapane +- @waiting-for-dev + +### Based on + +Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup \ No newline at end of file diff --git a/elixir/scrabble-score/lib/scrabble.ex b/elixir/scrabble-score/lib/scrabble.ex new file mode 100644 index 0000000..c66038e --- /dev/null +++ b/elixir/scrabble-score/lib/scrabble.ex @@ -0,0 +1,21 @@ +defmodule Scrabble do + defp char_score(char) when char in ~w(A E I O U L N R S T), do: 1 + defp char_score(char) when char in ~w(D G), do: 2 + defp char_score(char) when char in ~w(B C M P), do: 3 + defp char_score(char) when char in ~w(F H V W Y), do: 4 + defp char_score(char) when char in ~w(K), do: 5 + defp char_score(char) when char in ~w(J X), do: 8 + defp char_score(char) when char in ~w(Q Z), do: 10 + defp char_score(_char), do: 0 + + @doc """ + Calculate the scrabble score for the word. + """ + @spec score(String.t()) :: non_neg_integer + def score(word) do + word + |> String.upcase() + |> String.graphemes() + |> Enum.reduce(0, fn char, score -> score + char_score(char) end) + end +end diff --git a/elixir/scrabble-score/mix.exs b/elixir/scrabble-score/mix.exs new file mode 100644 index 0000000..3f265de --- /dev/null +++ b/elixir/scrabble-score/mix.exs @@ -0,0 +1,28 @@ +defmodule Scrabble.MixProject do + use Mix.Project + + def project do + [ + app: :scrabble, + version: "0.1.0", + # elixir: "~> 1.8", + 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/scrabble-score/test/scrabble_test.exs b/elixir/scrabble-score/test/scrabble_test.exs new file mode 100644 index 0000000..aa44155 --- /dev/null +++ b/elixir/scrabble-score/test/scrabble_test.exs @@ -0,0 +1,47 @@ +defmodule ScrabbleTest do + use ExUnit.Case + + test "empty word scores zero" do + assert Scrabble.score("") == 0 + end + + test "whitespace scores zero" do + assert Scrabble.score(" \t\n") == 0 + end + + test "uppercase letter" do + assert Scrabble.score("A") == 1 + end + + test "valuable letter" do + assert Scrabble.score("f") == 4 + end + + test "short word" do + assert Scrabble.score("at") == 2 + end + + test "short, valuable word" do + assert Scrabble.score("zoo") == 12 + end + + test "medium word" do + assert Scrabble.score("street") == 6 + end + + test "medium, valuable word" do + assert Scrabble.score("quirky") == 22 + end + + test "long, mixed-case word" do + assert Scrabble.score("OxyphenButazone") == 41 + end + + test "english-like word" do + assert Scrabble.score("pinata") == 8 + end + + test "entire alphabet available" do + assert Scrabble.score("abcdefghijklmnopqrstuvwxyz") == 87 + end +end diff --git a/elixir/scrabble-score/test/test_helper.exs b/elixir/scrabble-score/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/scrabble-score/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)