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
$ZWPACK
and$ZWBPACK
is also discouraged.
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.
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