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:
Plain: abcdefghijklmnopqrstuvwxyz Cipher: zyxwvutsrqponmlkjihgfedcba
It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic 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, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.
gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwltgives
Depending on the version of OCaml you installed the use of
frowned upon. Since version 4.03.0
String.lowercase is deprecated in favor of
String.lowercase_ascii. So instead of writing
String.lowercase "Hello, World!"
String.lowercase_ascii "Hello, World!"
See String documentation for more useful functions.
For installation and learning resources, refer to the exercism help page.
To work on the exercises, you will need
Base. Consult opam website for instructions on how to install
opam for your OS. Once
opam is installed open a terminal window and run the following command to install base:
opam install base
To run the tests you will need
OUnit. Install it using
opam install ounit
A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:
opam install utop
Consult utop for more detail.
The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.
If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
open OUnit2 open Atbash_cipher let ae exp got _test_ctxt = assert_equal ~printer:(fun x -> x) exp got let encode_tests = [ "encode yes" >:: ae "bvh" (encode "yes"); "encode no" >:: ae "ml" (encode "no"); "encode OMG" >:: ae "lnt" (encode "OMG"); "encode spaces" >:: ae "lnt" (encode "O M G"); "encode mindblowingly" >:: ae "nrmwy oldrm tob" (encode "mindblowingly"); "encode numbers" >:: ae "gvhgr mt123 gvhgr mt" (encode "Testing,1 2 3, testing."); "encode deep thought" >:: ae "gifgs rhurx grlm" (encode "Truth is fiction."); "encode all the letters" >:: ae "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" (encode "The quick brown fox jumps over the lazy dog."); ] let decode_tests = [ "decode exercism" >:: ae "exercism" (decode "vcvix rhn"); "decode a sentence" >:: ae "anobstacleisoftenasteppingstone" (decode "zmlyh gzxov rhlug vmzhg vkkrm thglm v"); "decode numbers" >:: ae "testing123testing" (decode "gvhgr mt123 gvhgr mt"); "decode all the letters" >:: ae "thequickbrownfoxjumpsoverthelazydog" (decode "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"); "decode with too many spaces" >:: ae "exercism" (decode "vc vix r hn"); "decode with no spaces" >:: ae "anobstacleisoftenasteppingstone" (decode "zmlyhgzxovrhlugvmzhgvkkrmthglmv"); ] let different_block_size_test = [ "encode mindblowingly with a different block size" >:: ae "n r m w y o l d r m t o b" (encode ~block_size:1 "mindblowingly"); ] let () = run_test_tt_main ( "atbash-cipher tests" >::: List.concat [encode_tests; decode_tests; different_block_size_test] )
let explode_string s = let rec exp i l = if i < 0 then l else exp(i-1) (s.[i] :: l) in exp (String.length s - 1)  let implode_string cs = String.concat "" (List.map (String.make 1) cs) let is_small_letter c = let ic = int_of_char c in ic >= int_of_char 'a' && ic <= int_of_char 'z' let is_digit c = let ic = int_of_char c in ic >= int_of_char '0' && ic <= int_of_char '9' let encode_char c = if is_digit c then Some c else if is_small_letter c then Some (char_of_int (int_of_char 'z' - int_of_char c + int_of_char 'a')) else None (* Encoding from English to atbash cipher. The default for block_size is 5 *) let encode ?block_size str = let block_size = match block_size with None -> 5 | Some x -> x in let rec worker chars i = match chars, i with | , _ ->  | cs, 0 -> ' '::(worker cs block_size) | (c::cs), i -> match encode_char c with | None -> worker cs i | Some e -> e::(worker cs (i-1)) in worker (String.lowercase_ascii str |> explode_string) block_size |> implode_string |> String.trim (* Decoding from atbash to English *) let decode str = encode ~block_size:(String.length str) str
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.