June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
63
Task/Run-length-encoding/Elena/run-length-encoding.elena
Normal file
63
Task/Run-length-encoding/Elena/run-length-encoding.elena
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import system'text.
|
||||
import system'routines.
|
||||
import extensions.
|
||||
|
||||
singleton compressor
|
||||
{
|
||||
literal compress(literal s)
|
||||
[
|
||||
type<TextBuilder> tb := TextBuilder new.
|
||||
int count := 0.
|
||||
char current := s[0].
|
||||
s forEach(:ch)
|
||||
[
|
||||
if (ch == current)
|
||||
[
|
||||
count += 1
|
||||
];
|
||||
[
|
||||
tb writeFormatted("{0}{1}",count,current).
|
||||
count := 1.
|
||||
current := ch
|
||||
]
|
||||
].
|
||||
|
||||
tb writeFormatted("{0}{1}",count,current).
|
||||
|
||||
^ tb
|
||||
]
|
||||
|
||||
literal decompress(literal s)
|
||||
[
|
||||
type<TextBuilder> tb := TextBuilder new.
|
||||
char current := $0.
|
||||
var a := String new.
|
||||
s forEach(:ch)
|
||||
[
|
||||
current := ch.
|
||||
if (current isDigit)
|
||||
[
|
||||
a append(ch)
|
||||
];
|
||||
[
|
||||
int count := a toInt.
|
||||
a clear.
|
||||
|
||||
tb fill(current,count).
|
||||
]
|
||||
].
|
||||
|
||||
^ tb
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var s := "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW".
|
||||
|
||||
s := compressor compress(s).
|
||||
console printLine(s).
|
||||
|
||||
s := compressor decompress(s).
|
||||
console printLine(s).
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue