Sha256: 89533ef249fd64aa0871eb33e0ff03b7ebfa677e54d4469f1c25758ce6127555
Contents?: true
Size: 1.09 KB
Versions: 263
Compression:
Stored size: 1.09 KB
Contents
module RunLengthEncoding open System let digitToInt c = int c - int '0' let partitionConsecutive list = let folder item acc = match acc with | (x::xs)::ys when x = item -> (item::x::xs) :: ys | _ -> [item] :: acc List.foldBack folder list [] let encode (input: string) = let encodePartition (partition: char list) = match partition with | [x] -> string x | x::_ -> sprintf "%i%O" (List.length partition) x | _ -> failwith "Can't encode empty partition" input.ToCharArray() |> List.ofArray |> partitionConsecutive |> List.map encodePartition |> List.reduce (+) let decode (input: string) = let folder ((decoded: string), (count: int option)) item = let updatedCount = Option.fold (fun acc x -> acc + x * 10) (digitToInt item) count |> Some let updateDecoded = Option.fold (fun acc x -> acc + new String(item, x - 1)) (decoded + string item) count if Char.IsDigit item then (decoded, updatedCount) else (updateDecoded, None) input |> Seq.fold folder ("", None) |> fst
Version data entries
263 entries across 263 versions & 1 rubygems