Sha256: ab9112d429d6c33346a98bd0899342d972d8123cea7de73853983147613eef3a

Contents?: true

Size: 1.06 KB

Versions: 284

Compression:

Stored size: 1.06 KB

Contents

module CryptoSquare

open System

let charsToString chars = new String(chars |> Array.ofSeq)

let chunksOfSize n seq = 
    seq 
    |> Seq.mapi(fun i x -> i / n, x)
    |> Seq.groupBy fst
    |> Seq.map (fun (_, g) -> g |> Seq.map snd |> charsToString)

let transpose seq = 
    seq
    |> Seq.collect(fun s -> s |> Seq.mapi(fun i e -> (i, e)))
    |> Seq.groupBy(fst)
    |> Seq.map(fun (_, s) -> s |> Seq.map snd |> charsToString)    

let normalizePlaintext (input: string) = seq { for c in input do if Char.IsLetterOrDigit c then yield Char.ToLowerInvariant c } |> charsToString

let size (input: string) = 
    input 
    |> normalizePlaintext 
    |> String.length 
    |> float 
    |> Math.Sqrt 
    |> ceil 
    |> int

let plaintextSegments (input: string) = 
    chunksOfSize (size input) (normalizePlaintext input) 
    |> List.ofSeq

let ciphertext (input: string) = 
    input 
    |> plaintextSegments 
    |> transpose 
    |> String.concat ""

let normalizeCiphertext (input: string) = 
    input 
    |> plaintextSegments 
    |> transpose 
    |> String.concat " "

Version data entries

284 entries across 262 versions & 1 rubygems

Version Path
trackler-2.2.1.6 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.5 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.4 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.3 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.2 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.1 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.0 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.6 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.5 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.4 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.3 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.2 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.1 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.0.0 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.55 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.54 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.53 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.52 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.51 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.1.0.50 tracks/fsharp/exercises/crypto-square/Example.fs