rotaitional_chiper

This commit is contained in:
Danil Negrienko 2024-06-29 00:20:33 -04:00
parent 26a8976b52
commit 9adeef7c61
10 changed files with 337 additions and 0 deletions

View File

@ -0,0 +1,26 @@
{
"authors": [
"DoggettCK"
],
"contributors": [
"angelikatyborska",
"Cohen-Carlisle",
"devonestes",
"neenjaw",
"sotojuan"
],
"files": {
"solution": [
"lib/rotational_cipher.ex"
],
"test": [
"test/rotational_cipher_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"rotational-cipher","id":"bd76a5d1822947e48b10bad622ce9d75","url":"https://exercism.org/tracks/elixir/exercises/rotational-cipher","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

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

24
elixir/rotational-cipher/.gitignore vendored Normal file
View 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").
rotational_cipher-*.tar

View File

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

View File

@ -0,0 +1,52 @@
# Rotational Cipher
Welcome to Rotational 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 rotational cipher, also sometimes called the Caesar cipher.
The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`.
Using a key of `0` or `26` will always yield the same output due to modular arithmetic.
The letter is shifted for as many values as the value of the key.
The general notation for rotational ciphers is `ROT + <key>`.
The most commonly used rotational cipher is `ROT13`.
A `ROT13` on the Latin alphabet would be as follows:
```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
```
It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.
Ciphertext is written out in the same formatting as the input including spaces and punctuation.
## Examples
- ROT5 `omg` gives `trl`
- ROT0 `c` gives `c`
- ROT26 `Cool` gives `Cool`
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
## Source
### Created by
- @DoggettCK
### Contributed to by
- @angelikatyborska
- @Cohen-Carlisle
- @devonestes
- @neenjaw
- @sotojuan
### Based on
Wikipedia - https://en.wikipedia.org/wiki/Caesar_cipher

View File

@ -0,0 +1,60 @@
defmodule RotationalCipher do
@moduledoc """
Generates a simple Caesar cipher by rotating the alphabet by a given amount.
Use for that shifted alphabet generation function and transliteration table based on two alphabets.
"""
@alphabet ?a..?z
|> Enum.to_list()
|> to_string()
|> String.split("", trim: true)
# Generates a shifted alphabet based on the given shift amount.
# If swaped is true, the function will return a map with the index as the key and the character as the value,
# otherwise the function will return a map with the character as the key and the index as the value.
defp alphabet(shift \\ 0, swaped \\ false) when shift < 26 and shift >= 0 do
@alphabet
|> Enum.with_index(fn char, index ->
position = if index + shift >= 26, do: index + shift - 26, else: index + shift
if swaped == true, do: {position, char}, else: {char, position}
end)
|> Enum.into(%{})
end
# Generates a transliteration table based on the given shift amount.
defp transliterate_table(shift) do
transliterated = alphabet(shift, true)
alphabet()
|> Enum.reduce(%{}, fn {char, index}, acc ->
Map.merge(acc, %{
transliterated[index] => char,
String.upcase(transliterated[index]) => String.upcase(char)
})
end)
end
defp rotate_char(char, table), do: table[char] || char
@doc """
Given a plaintext and amount to shift by, return a rotated string.
Example:
iex> RotationalCipher.rotate("Attack at dawn", 13)
"Nggnpx ng qnja"
"""
@spec rotate(text :: String.t(), shift :: integer) :: String.t()
def rotate(text, shift) when shift == 0 or shift == 26, do: text
def rotate(text, shift) do
table = transliterate_table(shift)
text
|> String.graphemes()
|> Enum.map(&rotate_char(&1, table))
|> Enum.join()
end
end

View File

@ -0,0 +1,28 @@
defmodule RotationalCipher.MixProject do
use Mix.Project
def project do
[
app: :rotational_cipher,
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

View File

@ -0,0 +1,65 @@
defmodule RotationalCipherTest do
use ExUnit.Case
test "rotate a by 1" do
plaintext = "a"
shift = 1
assert RotationalCipher.rotate(plaintext, shift) == "b"
end
test "rotate a by 26, same output as input" do
plaintext = "a"
shift = 26
assert RotationalCipher.rotate(plaintext, shift) == "a"
end
test "rotate a by 0, same output as input" do
plaintext = "a"
shift = 0
assert RotationalCipher.rotate(plaintext, shift) == "a"
end
test "rotate m by 13" do
plaintext = "m"
shift = 13
assert RotationalCipher.rotate(plaintext, shift) == "z"
end
test "rotate n by 13 with wrap around alphabet" do
plaintext = "n"
shift = 13
assert RotationalCipher.rotate(plaintext, shift) == "a"
end
test "rotate capital letters" do
plaintext = "OMG"
shift = 5
assert RotationalCipher.rotate(plaintext, shift) == "TRL"
end
test "rotate spaces" do
plaintext = "O M G"
shift = 5
assert RotationalCipher.rotate(plaintext, shift) == "T R L"
end
test "rotate numbers" do
plaintext = "Testing 1 2 3 testing"
shift = 4
assert RotationalCipher.rotate(plaintext, shift) == "Xiwxmrk 1 2 3 xiwxmrk"
end
test "rotate punctuation" do
plaintext = "Let's eat, Grandma!"
shift = 21
assert RotationalCipher.rotate(plaintext, shift) == "Gzo'n zvo, Bmviyhv!"
end
test "rotate all letters" do
plaintext = "The quick brown fox jumps over the lazy dog."
shift = 13
assert RotationalCipher.rotate(plaintext, shift) ==
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)