Run-length encoding: Difference between revisions

→‎{{header|PARI/GP}}: rewrote solution, previous one was incomplete and incorrect
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}}==
<lang parigp>encoderle(strings)={
This is only a partial solution; decode has not been written yet.
if(s=="", return(s));
my(v=Vec(s),cur=v[1],ct=1,out="");
v=concat(v,99); \\ sentinel
for(xi=2,#stringv,
if(v[i]==cur,
);ct++
,
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")
}elr(%)</lang>
Output:
<pre>%1 = "12W1B12W3B24W1B14W"
 
%2 = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"</pre>
<lang parigp>encode(string)={
my(number=1,string=eval(Vec(Str(string))),Letter=string[1]);
for(x=2,#string,
if(string[x]==Letter,
number+=1,
print1("("number")"Letter);
Letter=string[x];
number=0)
);
}</lang>
 
=={{header|Perl}}==