mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-11 10:07:13 -06:00
acronym elixir solution
This commit is contained in:
1
elixir/acronym/.exercism/metadata.json
Normal file
1
elixir/acronym/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"elixir","exercise":"acronym","id":"f0e509924ee3483c83a8e3f370eade53","url":"https://exercism.io/my/solutions/f0e509924ee3483c83a8e3f370eade53","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
4
elixir/acronym/.formatter.exs
Normal file
4
elixir/acronym/.formatter.exs
Normal file
@@ -0,0 +1,4 @@
|
||||
# Used by "mix format"
|
||||
[
|
||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||
]
|
||||
24
elixir/acronym/.gitignore
vendored
Normal file
24
elixir/acronym/.gitignore
vendored
Normal file
@@ -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").
|
||||
acronym-*.tar
|
||||
|
||||
50
elixir/acronym/README.md
Normal file
50
elixir/acronym/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# Acronym
|
||||
|
||||
Convert a phrase to its acronym.
|
||||
|
||||
Techies love their TLA (Three Letter Acronyms)!
|
||||
|
||||
Help generate some jargon by writing a program that converts a long name
|
||||
like Portable Network Graphics to its acronym (PNG).
|
||||
|
||||
## Running tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ mix test
|
||||
```
|
||||
|
||||
### Pending tests
|
||||
|
||||
In the test suites, all but the first test have been 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
|
||||
```
|
||||
|
||||
Or, you can enable all the tests by commenting out the
|
||||
`ExUnit.configure` line in the test suite.
|
||||
|
||||
```elixir
|
||||
# ExUnit.configure exclude: :pending, trace: true
|
||||
```
|
||||
|
||||
If you're stuck on something, it may help to look at some of
|
||||
the [available resources](https://exercism.io/tracks/elixir/resources)
|
||||
out there where answers might be found.
|
||||
|
||||
## Source
|
||||
|
||||
Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
14
elixir/acronym/lib/acronym.ex
Normal file
14
elixir/acronym/lib/acronym.ex
Normal file
@@ -0,0 +1,14 @@
|
||||
defmodule Acronym do
|
||||
@doc """
|
||||
Generate an acronym from a string.
|
||||
"This is a string" => "TIAS"
|
||||
"""
|
||||
@spec abbreviate(String.t()) :: String.t()
|
||||
def abbreviate(string) do
|
||||
string
|
||||
|> String.split(~r/\s|[-]|(?<=[a-z])-(?=[A-Z])|(?<=[a-z])(?=[A-Z])/)
|
||||
|> Enum.filter(&String.match?(&1, ~r/^\w/))
|
||||
|> Enum.map(&String.upcase(String.at(String.trim(&1, "_"), 0)))
|
||||
|> Enum.join("")
|
||||
end
|
||||
end
|
||||
28
elixir/acronym/mix.exs
Normal file
28
elixir/acronym/mix.exs
Normal file
@@ -0,0 +1,28 @@
|
||||
defmodule Acronym.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :acronym,
|
||||
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
|
||||
55
elixir/acronym/test/acronym_test.exs
Normal file
55
elixir/acronym/test/acronym_test.exs
Normal file
@@ -0,0 +1,55 @@
|
||||
defmodule AcronymTest do
|
||||
use ExUnit.Case
|
||||
|
||||
test "it produces acronyms from title case" do
|
||||
assert Acronym.abbreviate("Portable Networks Graphic") === "PNG"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms from lower case" do
|
||||
assert Acronym.abbreviate("Ruby on Rails") === "ROR"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it ignores punctuation" do
|
||||
assert Acronym.abbreviate("First in, First out") === "FIFO"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms from phrases with acronyms" do
|
||||
assert Acronym.abbreviate("GNU Image Manipulation Program") === "GIMP"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms ignoring punctuation and casing" do
|
||||
assert Acronym.abbreviate("Complementary Metal-Oxide semiconductor") === "CMOS"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces a very long abbreviation" do
|
||||
assert Acronym.abbreviate(
|
||||
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
|
||||
) === "ROTFLSHTMDCOALM"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms from phrases with consecutive delimiters" do
|
||||
assert Acronym.abbreviate("Something - I made up from thin air") === "SIMUFTA"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms from phrases with apostrophes" do
|
||||
assert Acronym.abbreviate("Halley's Comet") === "HC"
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "it produces acronyms from phrases with underscore emphasis" do
|
||||
assert Acronym.abbreviate("The Road _Not_ Taken") === "TRNT"
|
||||
end
|
||||
|
||||
# Track specific test case: additional case
|
||||
@tag :pending
|
||||
test "it produces acronyms from inconsistent case" do
|
||||
assert Acronym.abbreviate("HyperText Markup Language") === "HTML"
|
||||
end
|
||||
end
|
||||
2
elixir/acronym/test/test_helper.exs
Normal file
2
elixir/acronym/test/test_helper.exs
Normal file
@@ -0,0 +1,2 @@
|
||||
ExUnit.start()
|
||||
# ExUnit.configure(exclude: :pending, trace: true)
|
||||
Reference in New Issue
Block a user