Discussion
· Feb 2, 2022

Code Golf: Paired Opposites

You will receive a string of comma-separated integers whose elements have both a negative and a positive value, except for one integer that is either only negative or only positive, our challenge will be to find that integer.
As usual shortest solution wins.

Input

"1,-1,2,-2,3"

Output

3

3 has no matching negative appearance

Note

Rules

  1. The signature of the contest entry MUST be:
Class CodeGolf.PairedOpposites
{

ClassMethod Solve(o As %String) As %Integer
{
}

}
  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 Solve(o)
{
  q ##class(myPackage.myClass).test(o)
}
  1. The use of $ZWPACK and $ZWBPACK is also discouraged.
Discussion (34)1
Log in or sign up to continue

I decided to check the size of the code using an officially allowed method.

ClassMethod length(
  class = {$classname()},
  method "Solve"As %Integer CodeMode = expression ]
{
##class(%Dictionary.MethodDefinition).IDKEYOpen(classmethod).Implementation.Size
}

So,

size = 46 (43)

ClassMethod Solve(As %StringAs %Integer
{
 f  s c=$p(o,",",$i(i)) q:'$lf($lfs(o),-c) c
}

size = 48 (45)

ClassMethod Solve(As %StringAs %Integer
{
 f  s c=$p(o,",",$i(i)) ret:'$lf($lfs(o),-cc
}