Anonymous user
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).*/▼
▲/*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
y=
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▼
do k=j+1 to Lx while substr(x,k,1)==c▼
▲ do j=1 to Lx /*warning! J is modified below.*/
▲ if \datatype(c,'m') then do;say "error!: data isn't alphabetic";exit 13; end
▲ r=0 /*R is NOT the number of chars. */
▲ do k=j+1 to Lx while substr(x,k,1)==c
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>
<pre style="
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
output= 11WB11W2B23WB13W
</pre>
===decoding===
<lang rexx>/*REXX program decodes string by using a run-length scheme (min len=2).*/
|