Created
July 2, 2021 20:09
-
-
Save Rafailong/dd072c673f1f95d1a23eca21cc91c2e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Atbash Cipher | |
The Atbash Cipher is simple: replace every letter with its “mirror” in the alphabet. | |
A is replaced by Z. B is replaced by Y. Etc. Write a function to calculate it. | |
Examples | |
(atbash "") ;=> "" | |
(atbash "hello") ;=> "svool" | |
(atbash "Clojure") ;=> "Xolqfiv" | |
(atbash "Yo!") ;=> "Bl!" | |
Please maintain capitalization and non-alphabetic characters. | |
*) | |
let pairWithMirror l = List.rev l |> List.zip l | |
let lowersWithMirror = pairWithMirror [ 'a' .. 'z' ] | |
let finder chr = | |
let predicate = | |
fun (o, _) -> o = (System.Char.ToLower chr) | |
List.tryFind predicate lowersWithMirror | |
|> Option.map snd | |
let folder str chr = | |
let mirror = | |
match (finder chr) with | |
| Some m when System.Char.IsUpper chr -> System.Char.ToUpper m | |
| Some m -> m | |
| _ -> chr | |
sprintf "%s%c" str mirror | |
let atbash (str: string) = Seq.fold folder System.String.Empty str | |
atbash "" = "" | |
&& atbash "hello" = "svool" | |
&& atbash "Clojure" = "Xolqfiv" | |
&& atbash "Yo!" = "Bl!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment