Anonymous user
Run-length encoding: Difference between revisions
m
→{{header|REXX}}: combined the version 1 encode and decode into one program, added/changed comments and whitespace.
m (added whitespace.) |
m (→{{header|REXX}}: combined the version 1 encode and decode into one program, added/changed comments and whitespace.) |
||
Line 4,137:
===version 1===
The task (input) rule was relaxed a bit as this program accepts upper─ and lowercase input.
An error message is generated if the input text is invalid.
Note that this REXX version (for encoding and decoding) uses a ''replication'' count, not the ''count'' of characters,
<br>so a replication count of '''11''' represents a count of '''12''' characters.
<lang rexx>/*REXX program encodes and displays a string by using a run─length encoding scheme. */
parse arg
if
if \datatype(c, 'M') then do; say "error!: data isn't alphabetic:" c; exit 13; end▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
r= 0 /*R: is NOT the number of characters. */▼
▲err: say;
do k=j+1 to Lx while substr(x, k, 1)==c /*while characters ≡ C. */▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
r= r + 1 /*bump the replication count for a char*/▼
RLE: procedure; parse arg x; $=
if
if r==0 then $= $ || c /*don't use R if it is equal to zero.*/
input= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW▼
output= 11WB11W2B23WB13W▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
RLD: procedure; parse arg x; $= /*$: is the output string (so far). */
do j=1 by 0 to Lx; c= substr(x, j, 1) /*obtain a character from run encoding.*/
do k=j+1 to Lx
n= substr(x, j, #) + 1
$= $ || copies( substr(x, k, 1), n) /*N: is now the number of characters.
▲ #= # + 1 /*bump the count of the numeric chars. */
▲ end /*k*/ /*#: the number of characters so far. */
▲ n= substr(x, j, #) + 1 /*#: is length of the encoded number. */
▲ j= j + # + 1 /*increment the DO loop index by D+1. */
▲say 'decoded=' $ /*stick a fork in it, we're all done. */</lang>
{{out|output|text= when using the default input:}}
<pre>
decoded= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>
|