codewars: solve 'anagram detection' problem

This commit is contained in:
Ivan R. 2024-06-12 16:32:43 +05:00
parent 4e32127e79
commit 1febd49b82
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
7 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
codewars/elixir/anagram/.gitignore vendored Normal file
View file

@ -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/

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1 @@
ExUnit.start()