Daily Challenge #225 - Square'n'Sum dev.to staff on April 14, 2020 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 ... Read full post Submit Preview Dismiss Collapse Expand Youssef Rabei Youssef Rabei Youssef Rabei Follow I'm an 18 years old Javascript enthusiast, Front-End Web Developer 👨💻, Musician 🎻 🎹 and a Photographer 📷, Who's probably making coffee ☕ right now. Location Egypt Education High School Senior Joined Dec 7, 2018 • Apr 14 '20 Dropdown menu Copy link Hide Javascript: const squareSum = numbers => numbers.map(num => num ** 2).reduce((a, b) => a + b, 0) Collapse Expand Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Follow Ultra-fullstack software developer. Python, JavaScript, C#, C. Location Earth Education I am a master of science Pronouns They/them Work Software Engineer Joined May 2, 2017 • Apr 15 '20 Dropdown menu Copy link Hide 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 Expand Vidit Sarkar Vidit Sarkar Vidit Sarkar Follow Studying Computer Science & Engineering. Prefer to write clean and easy-to-understand code. Languages I use Python, C++, C, Java Script and Java. Location India Education Bachelor of Computer Science & Engineering Joined Feb 10, 2020 • Apr 14 '20 Dropdown menu Copy link Hide 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 Expand Mihail Malo Mihail Malo Mihail Malo Follow Joined Jul 11, 2018 • Apr 14 '20 • Edited on Apr 14 • Edited Dropdown menu Copy link Hide 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 Expand sam sam sam Follow i don't like webdev. Email sam.raker@gmail.com Location Austin, Texas Work Software Engineer at Ground.Work Joined Dec 30, 2016 • Apr 14 '20 • Edited on Apr 14 • Edited Dropdown menu Copy link Hide 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 Expand Yufan Lou Yufan Lou Yufan Lou Follow Learning Rust and Haskell, tired of JavaScript. Thinks Ruby is awesome except that it is not cross-platform enough. 日本語 / 中文 OK. He/him. Email loganlyf@gmail.com Location NYC, NY, US Work Student Joined Oct 29, 2019 • Apr 14 '20 • Edited on Apr 14 • Edited Dropdown menu Copy link Hide 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 Expand kesprit kesprit kesprit Follow French Senior iOS Developper Location Lyon Work Senior iOS Developper Joined Oct 14, 2019 • Apr 15 '20 Dropdown menu Copy link Hide 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 Expand Pavitra Golchha Pavitra Golchha Pavitra Golchha Follow Location India Joined Jul 5, 2018 • Apr 14 '20 Dropdown menu Copy link Hide Python squareSum = lambda arr: sum([n ** 2 for n in arr]) Collapse Expand SavagePixie SavagePixie SavagePixie Follow Always learning new things. I love web development and coding in general. Joined Jul 23, 2019 • Apr 14 '20 Dropdown menu Copy link Hide Elixir def square_sum(n), do: n |> Enum.map(&(&1 * &1)) |> Enum.sum Collapse Expand Dharmen Shah Dharmen Shah Dharmen Shah Follow I work on Angular, React, Electron, JavaScript, CSS, SCSS, Bootstrap, NodeJS. Love to contribute to Open-Source Projects. Location Ahmedabad Education Bechelor of Technology Work Sr. Full-Stack Developer at Solvative Joined May 7, 2019 • Apr 14 '20 Dropdown menu Copy link Hide JS: function squareSum(numbers){ return numbers.map(a=>Math.pow(a,2)).reduce((a,b)=>a+b,0); }; Collapse Expand Pavitra Golchha Pavitra Golchha Pavitra Golchha Follow Location India Joined Jul 5, 2018 • Apr 14 '20 Dropdown menu Copy link Hide Kotlin val squareSum = { arr: List<Int> -> arr.map { it * it }.sum() } Code of Conduct • Report abuse
Javascript:
Trying to find a more... unconventional javascript answer :D
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.
Point free baby!
(Obviously this is probably too polymorphic, you could just limit it to
[Int] -> Int)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.
My swift solution :
Python
Elixir
JS:
Kotlin