# Square(n) Sum in Python

## The challenge

Complete the square sum function so that it squares each number passed into it and then sums the results together.

For example, for `[1, 2, 2]` it should return `9` because `1^2 + 2^2 + 2^2 = 9`.

Complete the square sum function so that it squares each number passed into it and then sums the results together.

For example, for `[1, 2, 2]` it should return `9` because `1^2 + 2^2 + 2^2 = 9`.

## The solution in Python

Option 1:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```def square_sum(numbers):
out = []
for i in numbers:
out.append(i**2)
return sum(out)
```Code language: Python (python)```

Option 2:

``````def square_sum(numbers):
return sum(x ** 2 for x in numbers)
```Code language: Python (python)```

Option 3:

``````def square_sum(numbers):
return sum(map(lambda x: x**2,numbers))
```Code language: Python (python)```

## Test cases to validate our solution

``````import test
from solution import square_sum

@test.describe("Fixed Tests")
def basic_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(square_sum([1,2]), 5)
test.assert_equals(square_sum([, 3, 4, 5]), 50)
test.assert_equals(square_sum([]), )
test.assert_equals(square_sum([-1,-2]), 5)
test.assert_equals(square_sum([-1,,1]), 2)
```Code language: Python (python)```

See also  A Quick Introduction to Asyncio in Python3
Tags:
Subscribe
Notify of 