Run-length encoding: Difference between revisions

→‎{{header|REXX}}: no need to output count 1
(jq)
(→‎{{header|REXX}}: no need to output count 1)
Line 2,967:
</lang>
 
--[[User:Walterpachl|Walterpachl]] ([[User talk:Walterpachl|talk]]) 07:58, 10 May 2015 (UTC)=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
The task (input) rule was relaxed a bit as this program accepts upper- and lowercase input.
===Encoding===
Line 3,027 ⟶ 3,028:
decoded= WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>
 
===version 2===
<lang rexx>s='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
Say ' s='s
enc=encode(s)
Say 'enc='enc
dec=decode(enc)
Say 'dec='dec
if dec==s Then Say 'OK'
Exit
 
encode: Procedure
Parse Arg s
c=left(s,1)
cnt=1
ol=''
Do i=2 To length(s)
If substr(s,i,1)=c Then
cnt=cnt+1
Else Do
Call o cnt||c
c=substr(s,i,1)
cnt=1
End
End
Call o cnt||c
Return ol
 
decode: Procedure
Parse Arg s
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ol=''
Do While s<>''
p=verify(s,abc,'M')
Parse Var s cnt =(p) c +1 s
Call o copies(c,cnt)
End
Return ol
 
o: ol=ol||arg(1)
Return</lang>
{{out}}
<pre> s=WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
enc=12W1B12W3B24W1B14W
dec=WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
OK</pre>
 
===version 3===
No need to output counts that are 1
<lang rexx>s='WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW'
Say ' s='s
enc=encode(s)
Say 'enc='enc
dec=decode(enc)
Say 'dec='dec
if dec==s Then Say 'OK'
Exit
 
encode: Procedure
Parse Arg s
c=left(s,1)
cnt=1
ol=''
Do i=2 To length(s)
If substr(s,i,1)=c Then
cnt=cnt+1
Else Do
If cnt=1 Then
Call o c
Else
Call o cnt||c
c=substr(s,i,1)
cnt=1
End
End
Call o cnt||c
Return ol
 
decode: Procedure
Parse Arg s
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ol=''
Do While s<>''
p=verify(s,abc,'M')
If pos(left(s,1),abc)>0 Then Do
Parse Var s c +1 s
Call o c
End
Else Do
Parse Var s cnt =(p) c +1 s
Call o copies(c,cnt)
End
End
Return ol
 
o: ol=ol||arg(1)
Return</lang>
{{out}}
<pre> s=WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
enc=12WB12W3B24WB14W
dec=WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
OK</pre>
 
=={{header|Ruby}}==
2,299

edits