Code Golf - Encoder
We need to send some coordinates to a spaceship through a laser beam. To do that we have to encode it, and beam it out into space. Your mission is to implement the encoder with a compression standard. As usual shortest solution wins.
Task
You will receive a string of comma-separated integers and you will return a new string of comma-separated integers and sequence descriptors.
Input
"0,2,4,5,5,5,5,5,3,4,5"
Output
"0-4/2,5*5,3-5"
Note
- Compression happens left to right
- A sequence of 2 or more identical numbers is shortened as
number*count- example:
"5,5,5"is compressed to"5*3"
- example:
- A sequence of 3 or more consecutive numbers is shortened as
first-last. This is true for both ascending and descending order- example:
"1,3,4,5"is compressed to"1,3-5"
- example:
- A sequence of 3 or more numbers with the same interval is shortened as
first-last/interval.- example:
"0,2,4,6"is compressed to"0-6/2"
- example:
- Use this code to check the result length
- You also can use this test case here
Rules
- The signature of the contest entry MUST be:
Class dc.golf.Encoder {
ClassMethod Compress(a)
{
}
}
- It is forbidden to modify class/signature, including but not limited to:
- Adding inheritance
- Setting default argument values
- Adding class elements (Parameters, Methods, Includes, etc).
- It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
ClassMethod Compress(a)
{
q ##class(myPackage.myClass).test(a)
}
- The use of
$ZWPACKand$ZWBPACKis also discouraged.
Comments
According to the above task, "...a string of comma-separated integers ...", inputs like
"3, 5, 7, 8, 10, 12, 14, 16" and / or
"3, 5, 7, 7, 7, 7, -8, -6, -4, -21,-21,-21, 77,77,77,77"are also valid? Those are all integers too.
@Julius Kavay negative integers are valid too and it will be tested
.png)
I got 485 on 24 lines.
I have 190 on 1 line.
UPD: Program can be reduced to 189, but at the same time efficiency will decrease. Therefore, I will stop on the value 190. Let us know when the contest ends and it will be possible to open the sources.
I think we can safely consider this contest closed. Honestly, it's more of a discussion than a contest.
Ok.
size = 190
ClassMethod Compress(s As %String) As %String
{
a s a=$p(s,",",$i(i)),d=$p(s,",",i+1)-a f c=1:1{q:d*c+a'=$p(s,",",i+c)} s q=$zabs(d),v=$s(c>2&d:"-"_(c-1*d+a)_$s(q=1:"",1:"/"_q),c>1&'d:"*"_c,1:0) s:v'=0 $p(s,",",i,i+c-1)=a_v q:a="" s g a
}size = 189
ClassMethod Compress(s As %String) As %String
{
f i=1:1:2e6{s a=$p(s,",",i),d=$p(s,",",i+1)-a f c=1:1{q:d*c+a'=$p(s,",",i+c)} s q=$zabs(d),v=$s(c>2&d:"-"_(c-1*d+a)_$s(q=1:"",1:"/"_q),c>1&'d:"*"_c,1:0) s:v'=0 $p(s,",",i,i+c-1)=a_v} q s
}Hi Vitaliy,
Out of curiosity I had a go at this. I can't get under 188 using my own ideas and code:
You could shave 2 characters off each of your versions by having a variable contain the comma:
185 - with an unusual use of $PIECE instead of $SELECT to save 2 characters, which would also shorten yours to 184