Run-length encoding: Difference between revisions

m
→‎encoding: changed comments and indentation, added a comment. -- ~~~~
m (→‎decoding: changed comments and indentation, added comments. -- ~~~~)
m (→‎encoding: changed comments and indentation, added a comment. -- ~~~~)
Line 2,501:
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
===encoding===
<lang rexx>/*REXX program encodes string by using a run-length scheme (min len=2).*/
<lang rexx>
/*REXX program encodes string by using a run-length scheme (min len=2).*/
 
parse arg x /*normally, input would be a file*/
def='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
if x=='' then x=def /*No input? Then use the default*/
rLx=0 length(x) /*Rget the islength NOTof the numberX of charsstring. */
Lx=length(x)
y='' /*Y is the output string (so far)*/
do j=1 to Lx /*warning! J is modified below.*/
c=substr(x,j,1) /*pick a character, check for err*/
if \datatype(c,'m') then do;say "error!: data isn't alphabetic";exit 13; end
j=j+ r=0 /*AR badis thingNOT tothe do,number butof simplechars. */
 
do k=j+1 to Lx while substr(x,k,1)==c
do j=1 to Lx /*warning! J is modified below.*/
c r=substr(x,j,r+1) /*pickR is a character,replication count. check for err*/
end /*jk*/
if \datatype(c,'m') then do;say "error!: data isn't alphabetic";exit 13; end
r=0 /*R is NOT the number of chars. */
 
if r==0 then Y = Y ||c c /*C wan't repeated, just OUT it.*/
do k=j+1 to Lx while substr(x,k,1)==c
r=r+1 else Y = Y || r || c /*Radd it isto athe replicationencoded countstring. */
j=j+r else Y=Y||r||c /*addA itbad thing to thedo, encodedbut stringsimple. */
end /*j*/
 
if r==0 then Y=Y||c /*C wan't repeated, just OUT it.*/
else Y=Y||r||c /*add it to the encoded string. */
j=j+r /*A bad thing to do, but simple. */
end /*j*/
 
say ' input=' x
say 'encoded=' y
/*stick a fork in it, we're done.*/</lang>
</lang>
Output '''output''' when using default input:'
<pre style="height:7ex;overflow:scroll">
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
output= 11WB11W2B23WB13W
</pre>
 
===decoding===
<lang rexx>/*REXX program decodes string by using a run-length scheme (min len=2).*/