From 79c17a8c5932902a4bf857e5a2a898d17d3f03c6 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Thu, 27 Jun 2024 03:45:38 -0400 Subject: [PATCH] pangram --- elixir/pangram/.exercism/config.json | 28 ++++++++++ elixir/pangram/.exercism/metadata.json | 1 + elixir/pangram/.formatter.exs | 4 ++ elixir/pangram/.gitignore | 24 +++++++++ elixir/pangram/HELP.md | 75 ++++++++++++++++++++++++++ elixir/pangram/README.md | 50 +++++++++++++++++ elixir/pangram/lib/pangram.ex | 26 +++++++++ elixir/pangram/mix.exs | 28 ++++++++++ elixir/pangram/test/pangram_test.exs | 62 +++++++++++++++++++++ elixir/pangram/test/test_helper.exs | 2 + 10 files changed, 300 insertions(+) create mode 100644 elixir/pangram/.exercism/config.json create mode 100644 elixir/pangram/.exercism/metadata.json create mode 100644 elixir/pangram/.formatter.exs create mode 100644 elixir/pangram/.gitignore create mode 100644 elixir/pangram/HELP.md create mode 100644 elixir/pangram/README.md create mode 100644 elixir/pangram/lib/pangram.ex create mode 100644 elixir/pangram/mix.exs create mode 100644 elixir/pangram/test/pangram_test.exs create mode 100644 elixir/pangram/test/test_helper.exs diff --git a/elixir/pangram/.exercism/config.json b/elixir/pangram/.exercism/config.json new file mode 100644 index 0000000..502eae8 --- /dev/null +++ b/elixir/pangram/.exercism/config.json @@ -0,0 +1,28 @@ +{ + "authors": [ + "tejasbubane" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "lpil", + "neenjaw", + "parkerl", + "sotojuan" + ], + "files": { + "solution": [ + "lib/pangram.ex" + ], + "test": [ + "test/pangram_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Determine if a sentence is a pangram.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Pangram" +} diff --git a/elixir/pangram/.exercism/metadata.json b/elixir/pangram/.exercism/metadata.json new file mode 100644 index 0000000..ed643f2 --- /dev/null +++ b/elixir/pangram/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"pangram","id":"67a81725638b43a6990b5de0007cd9d1","url":"https://exercism.org/tracks/elixir/exercises/pangram","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/pangram/.formatter.exs b/elixir/pangram/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/pangram/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/pangram/.gitignore b/elixir/pangram/.gitignore new file mode 100644 index 0000000..c4363e0 --- /dev/null +++ b/elixir/pangram/.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"). +pangram-*.tar + diff --git a/elixir/pangram/HELP.md b/elixir/pangram/HELP.md new file mode 100644 index 0000000..6abe7b9 --- /dev/null +++ b/elixir/pangram/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/pangram.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/pangram/README.md b/elixir/pangram/README.md new file mode 100644 index 0000000..97037c9 --- /dev/null +++ b/elixir/pangram/README.md @@ -0,0 +1,50 @@ +# Pangram + +Welcome to Pangram on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Introduction + +You work for a company that sells fonts through their website. +They'd like to show a different sentence each time someone views a font on their website. +To give a comprehensive sense of the font, the random sentences should use **all** the letters in the English alphabet. + +They're running a competition to get suggestions for sentences that they can use. +You're in charge of checking the submissions to see if they are valid. + +~~~~exercism/note +Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter". + +The best known English pangram is: + +> The quick brown fox jumps over the lazy dog. +~~~~ + +## Instructions + +Your task is to figure out if a sentence is a pangram. + +A pangram is a sentence using every letter of the alphabet at least once. +It is case insensitive, so it doesn't matter if a letter is lower-case (e.g. `k`) or upper-case (e.g. `K`). + +For this exercise, a sentence is a pangram if it contains each of the 26 letters in the English alphabet. + +## Source + +### Created by + +- @tejasbubane + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @lpil +- @neenjaw +- @parkerl +- @sotojuan + +### Based on + +Wikipedia - https://en.wikipedia.org/wiki/Pangram \ No newline at end of file diff --git a/elixir/pangram/lib/pangram.ex b/elixir/pangram/lib/pangram.ex new file mode 100644 index 0000000..e6c5f4d --- /dev/null +++ b/elixir/pangram/lib/pangram.ex @@ -0,0 +1,26 @@ +defmodule Pangram do + @doc """ + Determines if a word or sentence is a pangram. + A pangram is a sentence using every letter of the alphabet at least once. + + Returns a boolean. + + ## Examples + + iex> Pangram.pangram?("the quick brown fox jumps over the lazy dog") + true + + """ + + @alphabet Enum.to_list(?A..?Z) + + @spec pangram?(String.t()) :: boolean + def pangram?(sentence) do + letters = + sentence + |> String.upcase() + |> String.to_charlist() + + @alphabet -- letters == [] + end +end diff --git a/elixir/pangram/mix.exs b/elixir/pangram/mix.exs new file mode 100644 index 0000000..62f02d7 --- /dev/null +++ b/elixir/pangram/mix.exs @@ -0,0 +1,28 @@ +defmodule Pangram.MixProject do + use Mix.Project + + def project do + [ + app: :pangram, + 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/pangram/test/pangram_test.exs b/elixir/pangram/test/pangram_test.exs new file mode 100644 index 0000000..0df7aa6 --- /dev/null +++ b/elixir/pangram/test/pangram_test.exs @@ -0,0 +1,62 @@ +defmodule PangramTest do + use ExUnit.Case + + # @tag :pending + test "empty sentence" do + refute Pangram.pangram?("") + end + + test "prefect lower case" do + assert Pangram.pangram?("abcdefghijklmnopqrstuvwxyz") + end + + test "pangram with only lower case" do + assert Pangram.pangram?("the quick brown fox jumps over the lazy dog") + end + + test "missing character 'x'" do + refute Pangram.pangram?("a quick movement of the enemy will jeopardize five gunboats") + end + + test "another missing character 'x'" do + refute Pangram.pangram?("the quick brown fish jumps over the lazy dog") + end + + test "missing character 'h'" do + refute Pangram.pangram?("five boxing wizards jump quickly at it") + end + + test "pangram with underscores" do + assert Pangram.pangram?("the_quick_brown_fox_jumps_over_the_lazy_dog") + end + + test "pangram with numbers" do + assert Pangram.pangram?("the 1 quick brown fox jumps over the 2 lazy dogs") + end + + test "missing letters replaced by numbers" do + refute Pangram.pangram?("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog") + end + + test "pangram with mixed case and punctuation" do + assert Pangram.pangram?("\"Five quacking Zephyrs jolt my wax bed.\"") + end + + test "case insensitive" do + assert Pangram.pangram?("the quick brown fox jumps over the lazy DOG") + end + + test "a-m and A-M are 26 different characters but not a pangram" do + refute Pangram.pangram?("abcdefghijklm ABCDEFGHIJKLM") + end + + test "pangram with non ascii characters" do + assert Pangram.pangram?("Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.") + end + + test "pangram in alphabet other than ASCII" do + refute Pangram.pangram?( + "Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства." + ) + end +end diff --git a/elixir/pangram/test/test_helper.exs b/elixir/pangram/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/pangram/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)