Discussion
Eduard Lebedyuk · Feb 2

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.
4
0 571
Discussion (34)1
Log in or sign up to continue

Special thanks for defining the clear rules and excluding 'dirty' tricks  yes

Straightforward one of 85 characters: 

ClassMethod Solve(As %String) As %Integer
{
  i=1:1:$l(o,","){x=$p(o,",",i) $d(y(-x)){y(-x)}else{y(x)=1}} $o(y(""))
}

Of course, this solution does not implement the rule "The only-positive or only-negative integer may appear more than once" :(

I think the problem as stated implies that there will always be at least one unpaired number: "... except for one integer that is either only negative or only positive"

Clever!

If only there was a way to get "remaining parts" length, in that case we could divide the sum you've got by a number of repetitions and get the answer.

ClassMethod Solve(o As %String) As %Integer

{

 s i=$l(o,","),j=$l(o,"-")-1,k=i-j-j s:k<0 k=k-j-j s m=$tr(o,",","+")_"/"_k

 q @m

}

Add's to GJ's effort and counts how many occurrences of the odd integer to be able to return exact answer (not a $ZABS answer).

Sadly, this solution doesn't quite work for negative unpaired integers.  For example "1,-1,-2"

 q @($tr(o,",","+")_"/"_$zabs(2*$l(o,"-")-$l(o,",")-2))

size = 65   return 0 if all pared;   { 0 is excluded for pairing }

ClassMethod Solve(o As %String) As %Integer
{
 s y=$lfs(o) f i=1:1:$ll(y) s c=$li(y,i) ret:'$lf(y,-c) c
 q 0
}

49 as we can iterate numbers, not list elements:

ClassMethod Solve(o As %String) As %Integer
{
 s y=$lfs(o) f i=-9:1 ret:$lf(y,i)&&'$lf(y,-i) i
}

NICE.  but runs forever if there is no unpared. 
And why starting with  -9 ?

And why starting with  -9 ?

Considering we need to hit anything from -9 to 9 where else should I start?

but it  fails if the unpaired is < -9 as in the ref.example

"-110,110,-38,-38,-62,62,-38,-38,-38"    and doesn't stop

this works unlimited but has no exit if none unmatched found
method length = 64,
and it shows only the abs. value  38 instead of -38

y=$lfs(o) i=1:1 a=$lf(y,i),b=$lf(y,-i) ret:a+b&'(a*b) i

Or

s y=$lfs(o) f i=1:1 ret:'$lf(y,i)+'$lf(y,-i)=1 i

...but this still only returns the absolute value, so not a valid solution either.

Yes, looked promising but there are issues as we cant iterate positive/negative: 1,-1,2,-2 and so on.

size 62

Class CodeGolf.PairedOpposites
{ ClassMethod Solve(As %String) As %Integer
{
res=0 i=1:1:$l(o,",") res=$p(o,",",i)+res res
} }

Even if correct the syntax error occurred due to COS Syntax highlight: 

 s res=0 f i=1:1:$l(o,",") s res=$p(o,",",i)+res
 q res

it fails on Ed's samples #3, 4, 5 as it summarizes unpaired numbers:

1,-1,2,-2,3,3 returns 6 vs 3
-110,110,-38,-38,-62,62,-38,-38,-38 returns -190 vs -38
-9,-105,-9,-9,-9,-9,105 returns -45 vs -9

It is a pity that the author left the previous task (Code Golf - Encoder) without attention, and the community ignored her (or did not notice;)

size = 49

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

48! while all applauds are still to Vitaly.

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

Good trick!

size = 47

ClassMethod Solve2(As %StringAs %Integer
{
c=$p(o,",",$i(i)) g:$lf($lfs(o),-cc
}

43, shortening of Vitaliy's

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

George James' solution is brilliant though.

FOR loop does not accept a quit with argument.

ERROR: CodeGolf.CHK.cls(7) :QUIT with arguments not allowed here c

but return is 5 chars more 

Whoops I was in a rush and did a command line test, stupid.

ret is a valid shorthand for return, so it's only +2 characters totaling 45.

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
}