Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/Caesar-cipher/Elixir/caesar-cipher.elixir
Normal file
18
Task/Caesar-cipher/Elixir/caesar-cipher.elixir
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
defmodule Caesar_cipher do
|
||||
defp set_map(map, range, key) do
|
||||
org = Enum.map(range, &List.to_string [&1])
|
||||
{a, b} = Enum.split(org, key)
|
||||
Enum.zip(org, b ++ a) |> Enum.into(map)
|
||||
end
|
||||
|
||||
def encode(text, key) do
|
||||
map = Map.new |> set_map(?a..?z, key) |> set_map(?A..?Z, key)
|
||||
String.graphemes(text) |> Enum.map_join(fn c -> Map.get(map, c, c) end)
|
||||
end
|
||||
end
|
||||
|
||||
text = "The five boxing wizards jump quickly"
|
||||
key = 3
|
||||
IO.puts "Original: #{text}"
|
||||
IO.puts "Encrypted: #{enc = Caesar_cipher.encode(text, key)}"
|
||||
IO.puts "Decrypted: #{Caesar_cipher.encode(enc, -key)}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue