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};}}
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, cloud computing, and automation expert. Enjoy books. Love my wife, dog, and cat, and think AI and Rust are pretty nifty
Location
Massachusetts, 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 !
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