anagram
This commit is contained in:
parent
e0c8beb31f
commit
fd86d9545f
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"authors": [
|
||||
"rubysolo"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewsardone",
|
||||
"angelikatyborska",
|
||||
"Br1ght0ne",
|
||||
"Cohen-Carlisle",
|
||||
"crazymykl",
|
||||
"dalexj",
|
||||
"devonestes",
|
||||
"henrik",
|
||||
"jeremy-w",
|
||||
"jinyeow",
|
||||
"kytrinyx",
|
||||
"lpil",
|
||||
"markijbema",
|
||||
"neenjaw",
|
||||
"parkerl",
|
||||
"pminten",
|
||||
"sotojuan",
|
||||
"Teapane",
|
||||
"tjcelaya",
|
||||
"waiting-for-dev"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/anagram.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/anagram_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"blurb": "Given a word and a list of possible anagrams, select the correct sublist.",
|
||||
"source": "Inspired by the Extreme Startup game",
|
||||
"source_url": "https://github.com/rchatley/extreme_startup"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"anagram","id":"0ffb252576fd4b4fad8cdd024185d2be","url":"https://exercism.org/tracks/elixir/exercises/anagram","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
|
@ -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").
|
||||
anagram-*.tar
|
||||
|
|
@ -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/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.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/anagram.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.
|
|
@ -0,0 +1,64 @@
|
|||
# Anagram
|
||||
|
||||
Welcome to Anagram on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Introduction
|
||||
|
||||
At a garage sale, you find a lovely vintage typewriter at a bargain price!
|
||||
Excitedly, you rush home, insert a sheet of paper, and start typing away.
|
||||
However, your excitement wanes when you examine the output: all words are garbled!
|
||||
For example, it prints "stop" instead of "post" and "least" instead of "stale."
|
||||
Carefully, you try again, but now it prints "spot" and "slate."
|
||||
After some experimentation, you find there is a random delay before each letter is printed, which messes up the order.
|
||||
You now understand why they sold it for so little money!
|
||||
|
||||
You realize this quirk allows you to generate anagrams, which are words formed by rearranging the letters of another word.
|
||||
Pleased with your finding, you spend the rest of the day generating hundreds of anagrams.
|
||||
|
||||
## Instructions
|
||||
|
||||
Your task is to, given a target word and a set of candidate words, to find the subset of the candidates that are anagrams of the target.
|
||||
|
||||
An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`.
|
||||
A word is _not_ its own anagram: for example, `"stop"` is not an anagram of `"stop"`.
|
||||
|
||||
The target and candidates are words of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`).
|
||||
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `StoP` is not an anagram of `sTOp`.
|
||||
The anagram set is the subset of the candidate set that are anagrams of the target (in any order).
|
||||
Words in the anagram set should have the same letter case as in the candidate set.
|
||||
|
||||
Given the target `"stone"` and candidates `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, `"Seton"`, the anagram set is `"tones"`, `"notes"`, `"Seton"`.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rubysolo
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewsardone
|
||||
- @angelikatyborska
|
||||
- @Br1ght0ne
|
||||
- @Cohen-Carlisle
|
||||
- @crazymykl
|
||||
- @dalexj
|
||||
- @devonestes
|
||||
- @henrik
|
||||
- @jeremy-w
|
||||
- @jinyeow
|
||||
- @kytrinyx
|
||||
- @lpil
|
||||
- @markijbema
|
||||
- @neenjaw
|
||||
- @parkerl
|
||||
- @pminten
|
||||
- @sotojuan
|
||||
- @Teapane
|
||||
- @tjcelaya
|
||||
- @waiting-for-dev
|
||||
|
||||
### Based on
|
||||
|
||||
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup
|
|
@ -0,0 +1,21 @@
|
|||
defmodule Anagram do
|
||||
@doc """
|
||||
Returns all candidates that are anagrams of, but not equal to, 'base'.
|
||||
"""
|
||||
@spec match(String.t(), [String.t()]) :: [String.t()]
|
||||
def match(base, candidates) do
|
||||
candidates
|
||||
|> Enum.filter(&anagram?(normalize(base), normalize(&1)))
|
||||
end
|
||||
|
||||
defp anagram?(base, base), do: false
|
||||
defp anagram?(base, candidate), do: hash(base) == hash(candidate)
|
||||
|
||||
@spec normalize(String.t()) :: String.t()
|
||||
defp normalize(string), do: string |> String.downcase() |> String.graphemes()
|
||||
|
||||
@spec hash(String.t()) :: Map.t()
|
||||
defp hash(string) do
|
||||
Enum.reduce(string, %{}, fn char, acc -> Map.update(acc, char, 1, &(&1 + 1)) end)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Anagram.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :anagram,
|
||||
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
|
|
@ -0,0 +1,100 @@
|
|||
defmodule AnagramTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "no matches" do
|
||||
matches = Anagram.match("diaper", ~w(hello world zombies pants))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "detects two anagrams" do
|
||||
matches = Anagram.match("solemn", ~w(lemons cherry melons))
|
||||
assert matches == ~w(lemons melons)
|
||||
end
|
||||
|
||||
test "does not detect anagram subsets" do
|
||||
matches = Anagram.match("good", ~w(dog goody))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "detects anagram" do
|
||||
matches = Anagram.match("listen", ~w(enlists google inlets banana))
|
||||
assert matches == ~w(inlets)
|
||||
end
|
||||
|
||||
test "detects three anagrams" do
|
||||
matches = Anagram.match("allergy", ~w(gallery ballerina regally clergy largely leading))
|
||||
assert matches == ~w(gallery regally largely)
|
||||
end
|
||||
|
||||
test "detects multiple anagrams with different case" do
|
||||
matches = Anagram.match("nose", ~w(Eons ONES))
|
||||
assert matches == ~w(Eons ONES)
|
||||
end
|
||||
|
||||
test "does not detect non-anagrams with identical checksum" do
|
||||
matches = Anagram.match("mass", ~w(last))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "detect anagrams case-insensitively" do
|
||||
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
||||
assert matches == ~w(Carthorse)
|
||||
end
|
||||
|
||||
test "detects anagrams using case-insensitive subject" do
|
||||
matches = Anagram.match("Orchestra", ~w(cashregister carthorse radishes))
|
||||
assert matches == ~w(carthorse)
|
||||
end
|
||||
|
||||
test "detects anagrams using case-insensitive possible matches" do
|
||||
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
||||
assert matches == ~w(Carthorse)
|
||||
end
|
||||
|
||||
test "does not detect an anagram if the original word is repeated" do
|
||||
matches = Anagram.match("go", ~w(goGoGO))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "anagrams must use all letters exactly once" do
|
||||
matches = Anagram.match("tapper", ~w(patter))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "words are not anagrams of themselves" do
|
||||
matches = Anagram.match("BANANA", ~w(BANANA))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "words are not anagrams of themselves even if letter case is partially different" do
|
||||
matches = Anagram.match("BANANA", ~w(Banana))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "words are not anagrams of themselves even if letter case is completely different" do
|
||||
matches = Anagram.match("BANANA", ~w(banana))
|
||||
assert matches == []
|
||||
end
|
||||
|
||||
test "words other than themselves can be anagrams" do
|
||||
matches = Anagram.match("LISTEN", ~w(Silent LISTEN))
|
||||
assert matches == ~w(Silent)
|
||||
end
|
||||
|
||||
test "handles case of greek letters" do
|
||||
matches = Anagram.match("ΑΒΓ", ~w(ΒΓΑ ΒΓΔ γβα αβγ))
|
||||
assert matches == ~w(ΒΓΑ γβα)
|
||||
end
|
||||
|
||||
test "different characters may have the same bytes" do
|
||||
matches =
|
||||
Anagram.match(
|
||||
# binary representation: <<0x61, 0xE2, 0xAC, 0x82>>
|
||||
"a⬂",
|
||||
# binary representation: <<0xE2, 0x82, 0xAC, 0x61>>
|
||||
["€a"]
|
||||
)
|
||||
|
||||
assert matches == []
|
||||
end
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue