codewars: solve 'anagram detection' problem
This commit is contained in:
parent
4e32127e79
commit
1febd49b82
7 changed files with 100 additions and 0 deletions
4
codewars/elixir/anagram/.formatter.exs
Normal file
4
codewars/elixir/anagram/.formatter.exs
Normal 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
26
codewars/elixir/anagram/.gitignore
vendored
Normal 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/
|
13
codewars/elixir/anagram/README.md
Normal file
13
codewars/elixir/anagram/README.md
Normal 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"
|
14
codewars/elixir/anagram/lib/anagram.ex
Normal file
14
codewars/elixir/anagram/lib/anagram.ex
Normal 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
|
28
codewars/elixir/anagram/mix.exs
Normal file
28
codewars/elixir/anagram/mix.exs
Normal 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
|
14
codewars/elixir/anagram/test/anagram_test.exs
Normal file
14
codewars/elixir/anagram/test/anagram_test.exs
Normal 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
|
1
codewars/elixir/anagram/test/test_helper.exs
Normal file
1
codewars/elixir/anagram/test/test_helper.exs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
ExUnit.start()
|
Loading…
Reference in a new issue