atbash
This commit is contained in:
parent
9425fd5368
commit
66f0e9c6f8
|
@ -0,0 +1,36 @@
|
|||
{
|
||||
"authors": [
|
||||
"rubysolo"
|
||||
],
|
||||
"contributors": [
|
||||
"andrewsardone",
|
||||
"angelikatyborska",
|
||||
"Br1ght0ne",
|
||||
"Cohen-Carlisle",
|
||||
"dalexj",
|
||||
"devonestes",
|
||||
"jinyeow",
|
||||
"kytrinyx",
|
||||
"lpil",
|
||||
"mhinz",
|
||||
"neenjaw",
|
||||
"parkerl",
|
||||
"sotojuan",
|
||||
"Teapane",
|
||||
"waiting-for-dev"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"lib/atbash.ex"
|
||||
],
|
||||
"test": [
|
||||
"test/atbash_test.exs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.ex"
|
||||
]
|
||||
},
|
||||
"blurb": "Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.",
|
||||
"source": "Wikipedia",
|
||||
"source_url": "https://en.wikipedia.org/wiki/Atbash"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"elixir","exercise":"atbash-cipher","id":"156913b0379c4b8886dff7de48ab370e","url":"https://exercism.org/tracks/elixir/exercises/atbash-cipher","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").
|
||||
atbash_cipher-*.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/atbash.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,60 @@
|
|||
# Atbash Cipher
|
||||
|
||||
Welcome to Atbash Cipher on Exercism's Elixir Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.
|
||||
|
||||
The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards.
|
||||
The first letter is replaced with the last letter, the second with the second-last, and so on.
|
||||
|
||||
An Atbash cipher for the Latin alphabet would be as follows:
|
||||
|
||||
```text
|
||||
Plain: abcdefghijklmnopqrstuvwxyz
|
||||
Cipher: zyxwvutsrqponmlkjihgfedcba
|
||||
```
|
||||
|
||||
It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
|
||||
However, this may not have been an issue in the cipher's time.
|
||||
|
||||
Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
|
||||
This is to make it harder to guess things based on word boundaries.
|
||||
All text will be encoded as lowercase letters.
|
||||
|
||||
## Examples
|
||||
|
||||
- Encoding `test` gives `gvhg`
|
||||
- Encoding `x123 yes` gives `c123b vh`
|
||||
- Decoding `gvhg` gives `test`
|
||||
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rubysolo
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @andrewsardone
|
||||
- @angelikatyborska
|
||||
- @Br1ght0ne
|
||||
- @Cohen-Carlisle
|
||||
- @dalexj
|
||||
- @devonestes
|
||||
- @jinyeow
|
||||
- @kytrinyx
|
||||
- @lpil
|
||||
- @mhinz
|
||||
- @neenjaw
|
||||
- @parkerl
|
||||
- @sotojuan
|
||||
- @Teapane
|
||||
- @waiting-for-dev
|
||||
|
||||
### Based on
|
||||
|
||||
Wikipedia - https://en.wikipedia.org/wiki/Atbash
|
|
@ -0,0 +1,48 @@
|
|||
defmodule Atbash do
|
||||
@letters ?a..?z
|
||||
|> Enum.to_list
|
||||
|> to_string
|
||||
|> String.graphemes()
|
||||
|
||||
@digits ?0..?9
|
||||
|> Enum.to_list
|
||||
|> to_string
|
||||
|> String.graphemes()
|
||||
|
||||
@table Map.new(Enum.zip(@letters, Enum.reverse(@letters)))
|
||||
|
||||
defguardp is_letter(char) when char in @letters
|
||||
defguardp is_digit(char) when char in @digits
|
||||
|
||||
@doc """
|
||||
Encode a given plaintext to the corresponding ciphertext
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atbash.encode("completely insecure")
|
||||
"xlnko vgvob rmhvx fiv"
|
||||
"""
|
||||
@spec encode(String.t()) :: String.t()
|
||||
def encode(plaintext) do
|
||||
plaintext
|
||||
|> transpose()
|
||||
|> String.graphemes()
|
||||
|> Enum.chunk_every(5)
|
||||
|> Enum.join(" ")
|
||||
end
|
||||
|
||||
@spec decode(String.t()) :: String.t()
|
||||
def decode(cipher), do: transpose(cipher)
|
||||
|
||||
defp transpose(string) do
|
||||
string
|
||||
|> String.downcase()
|
||||
|> String.graphemes()
|
||||
|> Enum.map(&code/1)
|
||||
|> Enum.join()
|
||||
end
|
||||
|
||||
defp code(char) when is_digit(char), do: char
|
||||
defp code(char) when is_letter(char), do: @table[char]
|
||||
defp code(_char), do: nil
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
defmodule Atbash.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :atbash,
|
||||
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,77 @@
|
|||
defmodule AtbashTest do
|
||||
use ExUnit.Case
|
||||
|
||||
describe "encode" do
|
||||
test "yes" do
|
||||
assert Atbash.encode("yes") == "bvh"
|
||||
end
|
||||
|
||||
test "no" do
|
||||
assert Atbash.encode("no") == "ml"
|
||||
end
|
||||
|
||||
test "OMG" do
|
||||
assert Atbash.encode("OMG") == "lnt"
|
||||
end
|
||||
|
||||
test "O M G" do
|
||||
assert Atbash.encode("O M G") == "lnt"
|
||||
end
|
||||
|
||||
test "mindblowingly" do
|
||||
assert Atbash.encode("mindblowingly") == "nrmwy oldrm tob"
|
||||
end
|
||||
|
||||
test "numbers" do
|
||||
assert Atbash.encode("Testing, 1 2 3, testing.") == "gvhgr mt123 gvhgr mt"
|
||||
end
|
||||
|
||||
test "deep thought" do
|
||||
assert Atbash.encode("Truth is fiction.") == "gifgs rhurx grlm"
|
||||
end
|
||||
|
||||
test "all the letters" do
|
||||
plaintext = "The quick brown fox jumps over the lazy dog."
|
||||
cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
|
||||
assert Atbash.encode(plaintext) == cipher
|
||||
end
|
||||
end
|
||||
|
||||
describe "decode" do
|
||||
test "exercism" do
|
||||
cipher = "vcvix rhn"
|
||||
plaintext = "exercism"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
|
||||
test "a sentence" do
|
||||
cipher = "zmlyh gzxov rhlug vmzhg vkkrm thglm v"
|
||||
plaintext = "anobstacleisoftenasteppingstone"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
|
||||
test "numbers" do
|
||||
cipher = "gvhgr mt123 gvhgr mt"
|
||||
plaintext = "testing123testing"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
|
||||
test "all the letters" do
|
||||
cipher = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"
|
||||
plaintext = "thequickbrownfoxjumpsoverthelazydog"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
|
||||
test "with too many spaces" do
|
||||
cipher = "vc vix r hn"
|
||||
plaintext = "exercism"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
|
||||
test "with no spaces" do
|
||||
cipher = "zmlyhgzxovrhlugvmzhgvkkrmthglmv"
|
||||
plaintext = "anobstacleisoftenasteppingstone"
|
||||
assert Atbash.decode(cipher) == plaintext
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,2 @@
|
|||
ExUnit.start()
|
||||
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue