4 people like it.
Like the snippet!
Affine Cipher
simple affineCipher translated from https://github.com/asweigart/codebreaker/blob/master/affineCipher.py not sure if actually correct.
I updated the below to make it easier to understand because I was bored, made some more changes, now pass in lambda as function to pxn instead of individual functions, as suggested by rwbarton for my haskell version.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
|
let flip f a b = f b a
let findModInverse a m =
[0..m]
|> Seq.filter (fun b -> a * b % m = 1)
|> Seq.head
let findIndex (s : string) (c : char) =
s.IndexOf c
let pxn s f =
Seq.map (findIndex s >> f >> flip Seq.nth s >> string)
let encode : (int -> int) -> (char seq -> string seq) =
pxn "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let enc a b =
encode (fun n -> a * n + b)
let dec a b =
encode (fun n -> n * (findModInverse a 26) - b)
let encrypt : string -> string = enc 1 2 >> String.concat ""
let decrypt : string -> string = dec 1 2 >> String.concat ""
assert (enc "TEST" = "VGUV")
assert (dec (enc "TEST") = "TEST")
|
val flip : f:('a -> 'b -> 'c) -> a:'b -> b:'a -> 'c
Full name: Script.flip
val f : ('a -> 'b -> 'c)
val a : 'b
val b : 'a
val findModInverse : a:int -> m:int -> int
Full name: Script.findModInverse
val a : int
val m : int
module Seq
from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.filter
val b : int
val head : source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.head
val findIndex : s:string -> c:char -> int
Full name: Script.findIndex
val s : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
val c : char
Multiple items
val char : value:'T -> char (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.char
--------------------
type char = System.Char
Full name: Microsoft.FSharp.Core.char
System.String.IndexOf(value: string) : int
System.String.IndexOf(value: char) : int
System.String.IndexOf(value: string, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int) : int
System.String.IndexOf(value: char, startIndex: int) : int
System.String.IndexOf(value: string, startIndex: int, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int, count: int) : int
System.String.IndexOf(value: char, startIndex: int, count: int) : int
System.String.IndexOf(value: string, startIndex: int, count: int, comparisonType: System.StringComparison) : int
val pxn : s:string -> f:(int -> int) -> (seq<char> -> seq<string>)
Full name: Script.pxn
val f : (int -> int)
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
val nth : index:int -> source:seq<'T> -> 'T
Full name: Microsoft.FSharp.Collections.Seq.nth
val encode : ((int -> int) -> seq<char> -> seq<string>)
Full name: Script.encode
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val enc : a:int -> b:int -> (seq<char> -> seq<string>)
Full name: Script.enc
val n : int
val dec : a:int -> b:int -> (seq<char> -> seq<string>)
Full name: Script.dec
val encrypt : (string -> string)
Full name: Script.encrypt
module String
from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string
Full name: Microsoft.FSharp.Core.String.concat
val decrypt : (string -> string)
Full name: Script.decrypt
More information