Run-length encoding: Difference between revisions
Content added Content deleted
m (→{{header|D}}: fixed indentation) |
(→{{header|PARI/GP}}: rewrote solution, previous one was incomplete and incorrect) |
||
Line 1,526:
{System.showInfo {RLDecode Enc}}</lang>
=={{header|PARI/GP}}==
if(s=="", return(s));
my(v=Vec(s),cur=v[1],ct=1,out="");
v=concat(v,99); \\ sentinel
if(v[i]==cur,
,
out=Str(out,ct,cur);
cur=v[i];
ct=1
)
);
out
};
elr(s)={
if(s=="", return(s));
my(v=Vec(s),ct=eval(v[1]),out="");
v=concat(v,99); \\ sentinel
for(i=2,#v,
if(v[i]>="0" && v[i]<="9",
ct=10*ct+eval(v[i])
,
for(j=1,ct,out=Str(out,v[i]));
ct=0
)
);
out
};
rle("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW")
Output:
<pre>%1 = "12W1B12W3B24W1B14W"
%2 = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</pre>
▲<lang parigp>encode(string)={
▲ for(x=2,#string,
▲ );
▲}</lang>
=={{header|Perl}}==
|