Run-length encoding: Difference between revisions

m
Added the Sidef language
(Nimrod -> Nim)
m (Added the Sidef language)
Line 3,226:
writeln(letterRleDecode("12W1B12W3B24W1B14W"));
end func;</lang>
 
=={{header|Sidef}}==
First solution:
<lang ruby>func encode(str) {
str.gsub(/((.)(\2*))/, {|a,b| "#{a.len}#{b}" });
}
 
func decode(str) {
str.gsub(/(\d+)(.)/, {|a,b| b * a.to_i });
};</lang>
{{out}}
<pre>12W1B12W3B24W1B14W</pre>
 
Second solution, encoding the length into a byte:
<lang ruby>func encode(str) {
str.gsub(/(.)(\1{0,254})/, {|a,b| b.len+1 -> chr + a});
}
 
func decode(str) {
var chars = str.chars;
var r = '';
0 .. (chars.len/2 - 1 -> int) each { |i|
r += (chars[2*i + 1] * str[2*i].ord);
};
return r;
};</lang>
{{out}}
<pre>"\fW\1B\fW\3B\30W\1B\16W"</pre>
 
=={{header|Smalltalk}}==
2,747

edits