DEV Community

dev.to staff
dev.to staff

Posted on

7 1

Daily Challenge #225 - Square'n'Sum

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!

Top comments (13)

Collapse
 
vidit1999 profile image
Vidit Sarkar

I am in love with Python.

square_sum = lambda lst: sum(map(int.__mul__ , *[lst]*2))

print(square_sum([1, 2, 2])) # output -> 9
print(square_sum([1, 2, 3, 4])) # output -> 30
print(square_sum([0, 3, 5, 7])) # output -> 83
print(square_sum([2, 2, 20])) # output -> 408
print(square_sum([])) # output -> 0
Collapse
 
youssefrabeiii profile image
Youssef Rabei

Javascript:

const squareSum = numbers => numbers.map(num => num ** 2).reduce((a, b) => a + b, 0)
Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

Trying to find a more... unconventional javascript answer :D

function squareSum(numbers)
{
  if(numbers.length > 0)
  {
    let n = numbers.pop();
    return square(n) + squareSum(numbers);
  }

  return 0;
}

let squares = [0, 1];
function square(n)
{
  let absn = Math.abs(n);
  if(typeof(squares[n]) !== 'undefined')
  {
    return squares[n];
  }

  let sqr = square(absn-1) + (absn-1) + absn;

  squares[n] = sqr;

  return sqr;
}
Collapse
 
qm3ster profile image
Mihail Malo • Edited

Sum of squares is the default example of the Rayon crate:

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- just change that!
         .map(|&i| i * i)
         .sum()
}

(this runs in parallel)

Apparently people here are really into onelinerization.

let sum_of_squares = |nums| nums.iter().map(|&i| i * i).sum();

No parallelism, but at least it's shorter than Python.

Collapse
 
louy2 profile image
Yufan Lou • Edited
squareSum = let square x = x * x in getSum . foldMap (Sum . square)

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.

Collapse
 
swizzard profile image
sam • Edited

Point free baby!

import Data.Monoid (Sum(..))

squareSum :: (Num c, Foldable t) => t c -> c
squareSum = getSum . foldMap (Sum . (^2))

(Obviously this is probably too polymorphic, you could just limit it to [Int] -> Int)

Collapse
 
kesprit profile image
kesprit

My swift solution :

func squareSum(_ array: [Int]) -> Int {
    Int(array.reduce(into: 0.0) { $0 += pow(Double($1), 2) })
}

squareSum([1, 2, 2]) // 9
squareSum([1, 2, 3, 4]) // 30
squareSum([0, 3, 5, 7]) // 83
squareSum([2, 2, 20]) // 408
squareSum([]) // 0
Collapse
 
aminnairi profile image
Amin

Elm

I really wish flip is part of the core library...

flip : (a -> b -> c) -> (b -> a -> c)
flip f a b = f b a

squareSum : List Int -> Int
squareSum integers =
    List.foldl (flip (^) 2 >> (+)) 0 integers
Collapse
 
pavi2410 profile image
Pavitra Golchha

Python

squareSum = lambda arr: sum([n ** 2 for n in arr])
Collapse
 
savagepixie profile image
SavagePixie

Elixir

def square_sum(n), do: n |> Enum.map(&(&1 * &1)) |> Enum.sum
Collapse
 
shhdharmen profile image
Dharmen Shah

JS:

function squareSum(numbers){
  return numbers.map(a=>Math.pow(a,2)).reduce((a,b)=>a+b,0);
};

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay