tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1,23 @@
/*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*/
Lx=length(x) /*get the length of the X string.*/
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
r=0 /*R is NOT the number of chars. */
do k=j+1 to Lx while substr(x,k,1)==c
r=r+1 /*R is a replication count. */
end /*k*/
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.*/

View file

@ -0,0 +1,24 @@
/*REXX program decodes string by using a run-length scheme (min len=2).*/
parse arg x /*normally, input would be a file*/
if x=='' then x='11WB11W2B23WB13W' /*No input? Then use the default*/
Lx=length(x) /*get the length of the X string.*/
y= /*Y is the output string (so far)*/
do j=1 to Lx /*warning! J is modified below.*/
c=substr(x,j,1)
if \datatype(c,'W') then do /*a loner char, simply add to OUT*/
y=y || c
iterate
end
d=1
do k=j+1 to Lx while datatype(substr(x,k,1),'w') /*look for #end*/
d=d+1 /*d is the number of digs so far.*/
end /*k*/
n=substr(x,j,d)+1 /*D is length of encoded number.*/
y=y || copies(substr(x,k,1),n) /*N is now the number of chars. */
j=j+d /*A bad thing to do, but simple. */
end /*j*/
say ' input=' x
say 'decoded=' y
/*stick a fork in it, we're done.*/