Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Run-length-encoding/Smalltalk/run-length-encoding-1.st
Normal file
34
Task/Run-length-encoding/Smalltalk/run-length-encoding-1.st
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
|compress decompress|
|
||||
compress := [:string |
|
||||
String streamContents:[:out |
|
||||
|count prev|
|
||||
|
||||
count := 0.
|
||||
(string,'*') "trick to avoid final run handling in loop"
|
||||
inject:nil
|
||||
into:[:prevChar :ch |
|
||||
ch ~= prevChar ifTrue:[
|
||||
count = 0 ifFalse:[
|
||||
count printOn:out.
|
||||
out nextPut:prevChar.
|
||||
count := 0.
|
||||
].
|
||||
].
|
||||
count := count + 1.
|
||||
ch
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
decompress := [:string |
|
||||
String streamContents:[:out |
|
||||
string readingStreamDo:[:in |
|
||||
[in atEnd] whileFalse:[
|
||||
|n ch|
|
||||
n := Integer readFrom:in.
|
||||
ch := in next.
|
||||
out next:n put:ch.
|
||||
]
|
||||
]
|
||||
].
|
||||
].
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
compress value:'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
-> '12W1B12W3B24W1B14W'
|
||||
|
||||
decompress value:'12W1B12W3B24W1B14W'
|
||||
-> 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
compress := [:string |
|
||||
String streamContents:[:out |
|
||||
string asRunArray runsDo:[:count :char |
|
||||
count printOn:out. out nextPut:char]]].
|
||||
Loading…
Add table
Add a link
Reference in a new issue