Discussion
Eduard Lebedyuk · Nov 17

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"
  • 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"
  • 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"
  • Use this code to check the result length
  • You also can use this test case here

Rules

  1. The signature of the contest entry MUST be:
Class dc.golf.Encoder {

ClassMethod Compress(a)
{
}

}
  1. 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).
  1. 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)
}
  1. The use of $ZWPACK and $ZWBPACK is also discouraged.
40
4 1 3 188
Log in or sign up to continue

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.