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.34 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.34 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.33 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.33 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.32 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.32 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.31 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.31 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.30 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.30 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.29 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.29 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.28 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.28 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.27 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.27 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.26 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.26 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.25 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.25 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs