usingSystem.Linq;usingSystem;publicclassKata{publicstaticint[]CountPositivesSumNegatives(int[]a){// guard clause for edge casesif(a==null||a.Length==0)returnnewint[0];intcount=0;intsum=0;// ".ToList()" is required to iterate the sequencea.Select(n=>n>0?count++:sum+=n).ToList();returnnew[]{count,sum};}}
Oh holy shit! I was looking at it for several minutes and it started making sense! Maybe because I thought through problem with @aspittel
, or maybe this APL is more sensible (the last one I saw, someone had spilled a bag of unicode across its source).
I'm using Ruby's comment syntax because IDK how to do it in APL
# Haskell uses arrows for assignment in `do` blocks.
# Whitespace delimited numbers are a thing in lisp.
# The high bar looks a lot like a minus sign, so:
A← # set into the variable A
A←1 2 3 ¯4 ¯5 ¯6 # the list of numbers: [1, 2, 3, -4, -5, -6]
# Haskell iplements + as a function that you can pass around
# The slash looks like and is used like a shell pipeline.
# Tilde is logical negation in many syntaxes.
# `A` in `A<0` is like SQL, which uses the
# table name to refer to that table's current row.
X← # set into the variable X
X←+/ # the sum of
X←+/(~ # the numbers which aren't
X←+/(~A<0)/ # negative
X←+/(~A<0)/A # from the list A
Y← # set into the variable Y
Y←+/ # the sum of
Y←+/(~ # the variables that aren't
Y←+/(~A>0)/ # positive
Y←+/(~A>0)/A # from the list A
O←X Y # set X and Y as the output
maybe this APL is more sensible (the last one I saw, someone had spilled a bag of unicode across its source).
That's the thing with first tries: You mostly write code which is relatively easy to understand. The APL symbol for comments is ⍝, by the way. And I have no idea how to write it without a Unicode table. ;-)
Thank you for your explanation. I finally understand wtf I was doing. (I know that I could probably have obfuscated it even more, "not negative" is longer than "positive", but I think I still won.)
letupdate(count,sum)value=ifvalue>0then(count+1),sumelsecount,(sum+value)// usage, start with 0 count/sum, update them for each valuelet(count,sum)=Array.foldupdate(0,0)inputArr
This returns a tuple instead of an array, which I believe is an improvement.
I'm Jake Cahill. Lifetime Pythonista, web scraping and automation expert. Enjoy books. Love my wife, dog, and cat, and think AI and Julia are pretty nifty
Location
Maine, USA
Education
A Master's patient mentorship and insatiable curiosity
Monday
Count of positives / sum of negatives (8 KYU):
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers.
link
Idk that this is an actual good Go solution - I'm not a Go expert so it can be hard for me to tell sometimes. It's so idiomatically verbose!
Solved it awhile ago (forgot about it).
Here is a C# answer.
And just re-solved it using JavaScript
Refactored to the code-golfiest thing ever with help of @joshcheek !
The golfiest version would probably be in Dyalog APL - even with newlines:
Output:
I will one day be able to look at APL without my brain leaking out of my ears.
Today is not that day.
This was my first time trying to actually do something with it. It was fun and it still sucked my brains out. (Both of them.)
Oh holy shit! I was looking at it for several minutes and it started making sense! Maybe because I thought through problem with @aspittel , or maybe this APL is more sensible (the last one I saw, someone had spilled a bag of unicode across its source).
I'm using Ruby's comment syntax because IDK how to do it in APL
That's the thing with first tries: You mostly write code which is relatively easy to understand. The APL symbol for comments is
⍝
, by the way. And I have no idea how to write it without a Unicode table. ;-)Thank you for your explanation. I finally understand wtf I was doing. (I know that I could probably have obfuscated it even more, "not negative" is longer than "positive", but I think I still won.)
Q/kdb+ solution (let l be the input list):
F#
This returns a tuple instead of an array, which I believe is an improvement.
Python 3 solution update. Posted to the wrong part of the discussion :P
Haskell
TypeScript
Usage:
Common Lisp FTW!