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
- The only-positive or only-negative integer may appear more than once
- 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 CodeGolf.PairedOpposites
{
ClassMethod Solve(o As %String) As %Integer
{
}
}
- 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 Solve(o)
{
q ##class(myPackage.myClass).test(o)
}
- The use of $ZWPACK and $ZWBPACK is also discouraged.
Special thanks for defining the clear rules and excluding 'dirty' tricks
Straightforward one of 85 characters:
q @$tr(o,",","+")
Of course, this solution does not implement the rule "The only-positive or only-negative integer may appear more than once" :(
and you get 0 if numbers are paired !
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"
Superb!
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 ?
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
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.
Is "1,-1,1" a valid input sequence?
No, there's no number without the opposite.
size 62
{ ClassMethod Solve(o As %String) As %Integer
{
s res=0 f i=1:1:$l(o,",") s res=$p(o,",",i)+res q 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:
-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
48! while all applauds are still to Vitaly.
Good trick!
size = 47
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.
So,
size = 46 (
43)size = 48 (
45)