Implement a function squareSum
so that it squares any number(s) passed into it and then adds the squares together.
For example, for [1, 2, 2]: it should return 9 because 1^2 + 2^2 + 2^2 = 9.
Tests
[1, 2, 3, 4]
[0, 3, 5, 7]
[2, 2, 20]
Good luck!
This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion
Javascript:
Trying to find a more... unconventional javascript answer :D
you can actually get rid of the
map
I am in love with Python.
Sum of squares is the default example of the Rayon crate:
(this runs in parallel)
Apparently people here are really into onelinerization.
No parallelism, but at least it's shorter than Python.
Btw, you gave "tests" but the tests don't have the supposed results. I know I can hand calculate but still they are not complete test cases.
Point free baby!
(Obviously this is probably too polymorphic, you could just limit it to
[Int] -> Int
)How about this?
Also point free and a bit easier on the eye. If we want to keep it single traversal, maybe
My swift solution :
Raku
With nothing else to do in lockdown, I made a Raku version too:
Or single-pass with
reduce
:Elm
I really wish flip is part of the core library...
Ruby:
A GolfScript solution:
Explanation: map (
%
) the block{. *}
over the input..
duplicates the element,*
multiplies the top 2 stack elements. Then fold (*
) a block performing addition{+}
over the result.Python
Kotlin
Elixir
JS: