From 1febd49b8272ee47f8697b1a6e35ec8c75defee0 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Wed, 12 Jun 2024 16:32:43 +0500 Subject: [PATCH] codewars: solve 'anagram detection' problem --- codewars/elixir/anagram/.formatter.exs | 4 +++ codewars/elixir/anagram/.gitignore | 26 +++++++++++++++++ codewars/elixir/anagram/README.md | 13 +++++++++ codewars/elixir/anagram/lib/anagram.ex | 14 ++++++++++ codewars/elixir/anagram/mix.exs | 28 +++++++++++++++++++ codewars/elixir/anagram/test/anagram_test.exs | 14 ++++++++++ codewars/elixir/anagram/test/test_helper.exs | 1 + 7 files changed, 100 insertions(+) create mode 100644 codewars/elixir/anagram/.formatter.exs create mode 100644 codewars/elixir/anagram/.gitignore create mode 100644 codewars/elixir/anagram/README.md create mode 100644 codewars/elixir/anagram/lib/anagram.ex create mode 100644 codewars/elixir/anagram/mix.exs create mode 100644 codewars/elixir/anagram/test/anagram_test.exs create mode 100644 codewars/elixir/anagram/test/test_helper.exs diff --git a/codewars/elixir/anagram/.formatter.exs b/codewars/elixir/anagram/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/codewars/elixir/anagram/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/codewars/elixir/anagram/.gitignore b/codewars/elixir/anagram/.gitignore new file mode 100644 index 0000000..e2a8783 --- /dev/null +++ b/codewars/elixir/anagram/.gitignore @@ -0,0 +1,26 @@ +# 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"). +anagram-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/codewars/elixir/anagram/README.md b/codewars/elixir/anagram/README.md new file mode 100644 index 0000000..c752e2a --- /dev/null +++ b/codewars/elixir/anagram/README.md @@ -0,0 +1,13 @@ +# Anagram + +An anagram is the result of rearranging the letters of a word to produce a new word. + +Note: anagrams are case insensitive. + +Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise. + + +## Examples + +- "foefet" is an anagram of "toffee" +- "Buckethead" is an anagram of "DeathCubeK" diff --git a/codewars/elixir/anagram/lib/anagram.ex b/codewars/elixir/anagram/lib/anagram.ex new file mode 100644 index 0000000..0ef893e --- /dev/null +++ b/codewars/elixir/anagram/lib/anagram.ex @@ -0,0 +1,14 @@ +defmodule Anagram do + @spec anagram?(a :: String.t(), b :: String.t()) :: boolean + def anagram?(a, b) do + Map.equal?(to_symbols(a), to_symbols(b)) + end + + @spec to_symbols(a :: String.t()) :: map() + defp to_symbols(a) do + a + |> String.downcase() + |> String.graphemes() + |> Enum.frequencies() + end +end diff --git a/codewars/elixir/anagram/mix.exs b/codewars/elixir/anagram/mix.exs new file mode 100644 index 0000000..d03b1ec --- /dev/null +++ b/codewars/elixir/anagram/mix.exs @@ -0,0 +1,28 @@ +defmodule Anagram.MixProject do + use Mix.Project + + def project do + [ + app: :anagram, + version: "0.1.0", + elixir: "~> 1.16", + 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/codewars/elixir/anagram/test/anagram_test.exs b/codewars/elixir/anagram/test/anagram_test.exs new file mode 100644 index 0000000..63ed1b3 --- /dev/null +++ b/codewars/elixir/anagram/test/anagram_test.exs @@ -0,0 +1,14 @@ +defmodule AnagramTest do + use ExUnit.Case + import Anagram + doctest Anagram + + test "some test description" do + assert anagram?("foefet", "toffee") === true + assert anagram?("Buckethead", "DeathCubeK") === true + assert anagram?("Twoo", "WooT") === true + assert anagram?("dumble", "bumble") === false + assert anagram?("ound", "round") === false + assert anagram?("apple", "pale") === false + end +end diff --git a/codewars/elixir/anagram/test/test_helper.exs b/codewars/elixir/anagram/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/codewars/elixir/anagram/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()