tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
37
Task/Run-length-encoding/Oz/run-length-encoding.oz
Normal file
37
Task/Run-length-encoding/Oz/run-length-encoding.oz
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
declare
|
||||
fun {RLEncode Xs}
|
||||
for G in {Group Xs} collect:C do
|
||||
{C {Length G}#G.1}
|
||||
end
|
||||
end
|
||||
|
||||
fun {RLDecode Xs}
|
||||
for C#Y in Xs append:Ap do
|
||||
{Ap {Replicate Y C}}
|
||||
end
|
||||
end
|
||||
|
||||
%% Helpers
|
||||
%% e.g. "1122" -> ["11" "22"]
|
||||
fun {Group Xs}
|
||||
case Xs of nil then nil
|
||||
[] X|Xr then
|
||||
Ys Zs
|
||||
{List.takeDropWhile Xr fun {$ W} W==X end ?Ys ?Zs}
|
||||
in
|
||||
(X|Ys) | {Group Zs}
|
||||
end
|
||||
end
|
||||
%% e.g. 3,4 -> [3 3 3 3]
|
||||
fun {Replicate X N}
|
||||
case N of 0 then nil
|
||||
else X|{Replicate X N-1}
|
||||
end
|
||||
end
|
||||
|
||||
Data = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"
|
||||
Enc = {RLEncode Data}
|
||||
in
|
||||
{System.showInfo Data}
|
||||
{Show Enc}
|
||||
{System.showInfo {RLDecode Enc}}
|
||||
Loading…
Add table
Add a link
Reference in a new issue