How to resolve the algorithm Straddling checkerboard step by step in the F# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Straddling checkerboard step by step in the F# programming language
Table of Contents
Problem Statement
Implement functions to encrypt and decrypt a message using the straddling checkerboard method. The checkerboard should take a 28 character alphabet (A-Z plus a full stop and an escape character) and two different numbers representing the blanks in the first row. The output will be a series of decimal digits. Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Straddling checkerboard step by step in the F# programming language
Source code in the fsharp programming language
(*
Encode and Decode using StraddlingCheckerboard
Nigel Galloway May 15th., 2017
*)
type G={n:char;i:char;g:System.Collections.Generic.Dictionary<(char*char),string>;e:System.Collections.Generic.Dictionary<char,string>}
member G.encode n=n|>Seq.map(fun n->if (n='/') then G.e.['/']+string n else match (G.e.TryGetValue(n)) with |(true,n)->n|(false,_)->G.e.['/']+string n)
member G.decode n =
let rec fn n = seq{ if not (Seq.isEmpty n)
then match (match Seq.head n with |g when g=G.n||g=G.i->(G.g.[(g,(Seq.item 1 n))],(Seq.skip 2 n))|g->(G.g.[('/',g)],(Seq.tail n))) with
|(a,b) when a="/"->yield string (Seq.head b); yield! fn (Seq.tail b)
|(a,b) ->yield a; yield! fn b
}
fn n
let G n i g e l z=
let a = new System.Collections.Generic.Dictionary<(char*char),string>()
let b = new System.Collections.Generic.Dictionary<char,string>()
Seq.iter2 (fun ng gn->a.[('/' ,char ng)]<-string gn;b.[gn]<-ng) (List.except [n;i] z) g
Seq.iter2 (fun ng gn->a.[(char n,char ng)]<-string gn;b.[gn]<-n+ng) z e
Seq.iter2 (fun ng gn->a.[(char i,char ng)]<-string gn;b.[gn]<-i+ng) z l
{n=char n;i=char i;g=a;e=b}
let N = G "2" "6" "ETAONRIS" "BCDFGHJKLM" "PQ/UVWXYZ." ["0";"1";"2";"3";"4";"5";"6";"7";"8";"9"]
N.decode "450582425181653945125016505180125423293721256216286286288653970163758524"|>Seq.iter(fun n->printf "%s" n);; //ONENIGHTITWASONTHETWENTIETHOFMARCH1888IWASRETURNING
N.encode "IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE"|>Seq.iter(fun n->printfn "%s" n);; //8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640
N.decode "8562 125062 658510762 62162962662562 65062 6507062 256352476762 26639162 20370286762 3288640"|>Seq.iter(fun n->printf "%s" n);; //IN THE WINTER 1965 WE WERE HUNGRY JUST BARELY ALIVE
You may also check:How to resolve the algorithm Haversine formula step by step in the zkl programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the zkl programming language
You may also check:How to resolve the algorithm Call a function step by step in the Lua programming language
You may also check:How to resolve the algorithm Record sound step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Digital root step by step in the Malbolge programming language