Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
18
Task/Run-length-encoding/F-Sharp/run-length-encoding.fs
Normal file
18
Task/Run-length-encoding/F-Sharp/run-length-encoding.fs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
open System
|
||||
open System.Text.RegularExpressions
|
||||
|
||||
let encode data =
|
||||
// encodeData : seq<'T> -> seq<int * 'T> i.e. Takes a sequence of 'T types and return a sequence of tuples containing the run length and an instance of 'T.
|
||||
let rec encodeData input =
|
||||
seq { if not (Seq.isEmpty input) then
|
||||
let head = Seq.head input
|
||||
let runLength = Seq.length (Seq.takeWhile ((=) head) input)
|
||||
yield runLength, head
|
||||
yield! encodeData (Seq.skip runLength input) }
|
||||
|
||||
encodeData data |> Seq.fold(fun acc (len, d) -> acc + len.ToString() + d.ToString()) ""
|
||||
|
||||
let decode str =
|
||||
[ for m in Regex.Matches(str, "(\d+)(.)") -> m ]
|
||||
|> List.map (fun m -> Int32.Parse(m.Groups.[1].Value), m.Groups.[2].Value)
|
||||
|> List.fold (fun acc (len, s) -> acc + String.replicate len s) ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue