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.44 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.44 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.43 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.43 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.42 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.42 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.41 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.41 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.40 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.40 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.39 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.39 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.38 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.38 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.37 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.37 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.36 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.36 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.35 tracks/fsharp/exercises/crypto-square/CryptoSquare.fs
trackler-2.2.1.35 tracks/fsharp/exercises/crypto-square/Example.fs