Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Run-length-encoding/OCaml/run-length-encoding-1.ocaml
Normal file
23
Task/Run-length-encoding/OCaml/run-length-encoding-1.ocaml
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
let encode str =
|
||||
let len = String.length str in
|
||||
let rec aux i acc =
|
||||
if i >= len then List.rev acc
|
||||
else
|
||||
let c1 = str.[i] in
|
||||
let rec aux2 j =
|
||||
if j >= len then (c1, j-i)
|
||||
else
|
||||
let c2 = str.[j] in
|
||||
if c1 = c2
|
||||
then aux2 (j+1)
|
||||
else (c1, j-i)
|
||||
in
|
||||
let (c,n) as t = aux2 (i+1) in
|
||||
aux (i+n) (t::acc)
|
||||
in
|
||||
aux 0 []
|
||||
;;
|
||||
|
||||
let decode lst =
|
||||
let l = List.map (fun (c,n) -> String.make n c) lst in
|
||||
(String.concat "" l)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let () =
|
||||
let e = encode "aaaaahhhhhhmmmmmmmuiiiiiiiaaaaaa" in
|
||||
List.iter (fun (c,n) ->
|
||||
Printf.printf " (%c, %d);\n" c n;
|
||||
) e;
|
||||
print_endline (decode [('a', 5); ('h', 6); ('m', 7); ('u', 1); ('i', 7); ('a', 6)]);
|
||||
;;
|
||||
17
Task/Run-length-encoding/OCaml/run-length-encoding-3.ocaml
Normal file
17
Task/Run-length-encoding/OCaml/run-length-encoding-3.ocaml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#load "str.cma";;
|
||||
|
||||
open Str
|
||||
|
||||
let encode =
|
||||
global_substitute (Str.regexp "\\(.\\)\\1*")
|
||||
(fun s -> string_of_int (String.length (matched_string s)) ^
|
||||
matched_group 1 s)
|
||||
|
||||
let decode =
|
||||
global_substitute (Str.regexp "\\([0-9]+\\)\\([^0-9]\\)")
|
||||
(fun s -> String.make (int_of_string (matched_group 1 s))
|
||||
(matched_group 2 s).[0])
|
||||
|
||||
let () =
|
||||
print_endline (encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW");
|
||||
print_endline (decode "12W1B12W3B24W1B14W");
|
||||
Loading…
Add table
Add a link
Reference in a new issue