Sha256: b50484dde57013d1d68325a6ce74b10cc15f3eeacc0a6fe0dd50879437bdd0cc

Contents?: true

Size: 1.1 KB

Versions: 134

Compression:

Stored size: 1.1 KB

Contents

module CryptoSquare

open System

let private charsToString chars = String(Array.ofSeq chars)

let private 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 private 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 private normalizedPlaintext (input: string) = seq { for c in input do if Char.IsLetterOrDigit c then yield Char.ToLowerInvariant c } |> charsToString

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

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

let ciphertext (input: string) = 
    let chunks = plaintextSegments input 
    let numberOfChunks = List.length chunks
    
    chunks
    |> transpose 
    |> Seq.map (fun x -> x.PadRight(numberOfChunks))
    |> String.concat " "

Version data entries

134 entries across 134 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.179 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.178 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.177 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.176 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.175 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.174 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.173 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.172 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.171 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.170 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.169 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.167 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.166 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.165 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.164 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.163 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.162 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.161 tracks/fsharp/exercises/crypto-square/Example.fs
trackler-2.2.1.160 tracks/fsharp/exercises/crypto-square/Example.fs