Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk · 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.

Comments

Robert Cemper · Feb 2, 2022

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

0
Alexey Maslov · Feb 2, 2022

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(""))}
0
George James  Feb 2, 2022 to George James

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

0
Robert Cemper  Feb 2, 2022 to George James

and you get 0 if numbers are paired !

0
George James  Feb 2, 2022 to Robert Cemper

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"

0
Eduard Lebedyuk  Feb 2, 2022 to George James

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.

0
Stuart Strickland  Feb 2, 2022 to Eduard Lebedyuk

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

}

0
Stuart Strickland  Feb 2, 2022 to Stuart Strickland

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).

0
George James  Feb 3, 2022 to Stuart Strickland

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

0
George James  Feb 3, 2022 to George James
 q @($tr(o,",","+")_"/"_$zabs(2*$l(o,"-")-$l(o,",")-2))
0
Robert Cemper · Feb 2, 2022

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
}
0
Eduard Lebedyuk  Feb 2, 2022 to Robert Cemper

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
}
0
Robert Cemper  Feb 2, 2022 to Eduard Lebedyuk

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

0
Eduard Lebedyuk  Feb 2, 2022 to Robert Cemper

And why starting with  -9 ?

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

0
Robert Cemper  Feb 2, 2022 to Eduard Lebedyuk

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

0
Robert Cemper  Feb 2, 2022 to Robert Cemper

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

0
George James  Feb 2, 2022 to Robert Cemper

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.

0
Eduard Lebedyuk  Feb 4, 2022 to George James

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

0
Eduard Lebedyuk  Feb 2, 2022 to Julius Kavay

No, there's no number without the opposite.

0
Paul Collins · Feb 2, 2022

size 62

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

0
Alexey Maslov  Feb 3, 2022 to Paul Collins

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

0
Vitaliy Serdtsev · Feb 3, 2022

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;)

0
Vitaliy Serdtsev · Feb 3, 2022

size = 49

ClassMethod Solve(As %StringAs %Integer
{
 i=1:1 c=$p(o,",",iret:'$lf($lfs(o),-cc
}
0
Robert Cemper  Feb 3, 2022 to Vitaliy Serdtsev

yes  GREAT !  covers all situations.

0
Alexey Maslov · Feb 3, 2022

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}
0
Vitaliy Serdtsev  Feb 3, 2022 to Alexey Maslov

Good trick!

size = 47

ClassMethod Solve2(As %StringAs %Integer
{
c=$p(o,",",$i(i)) g:$lf($lfs(o),-cc
}
0
David Underhill  Feb 9, 2022 to Vitaliy Serdtsev

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.

0
Robert Cemper  Feb 9, 2022 to David Underhill

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 

0
David Underhill  Feb 9, 2022 to Robert Cemper

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

0
Eduard Lebedyuk  Feb 9, 2022 to Robert Cemper

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

0
Vitaliy Serdtsev  Feb 9, 2022 to Eduard Lebedyuk

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
}
0