Thursday, December 7, 2023
HomeSoftware EngineeringSq.(n) Sum in Python

# Sq.(n) Sum in Python

## The problem#

Full the sq. sum operate in order that it squares every quantity handed into it after which sums the outcomes collectively.

For instance, for `[1, 2, 2]` it ought to return `9` as a result of `1^2 + 2^2 + 2^2 = 9`.

Full the sq. sum operate in order that it squares every quantity handed into it after which sums the outcomes collectively.

For instance, for `[1, 2, 2]` it ought to return `9` as a result of `1^2 + 2^2 + 2^2 = 9`.

Possibility 1:

``````def square_sum(numbers):
out = []
for i in numbers:
out.append(i**2)
return sum(out)
``````

Possibility 2:

``````def square_sum(numbers):
return sum(x ** 2 for x in numbers)
``````

Possibility 3:

``````def square_sum(numbers):
return sum(map(lambda x: x**2,numbers))
``````

## Check circumstances to validate our answer#

``````import check

@check.describe("Fastened Assessments")
def basic_tests():
@check.it('Fundamental Check Instances')
def basic_test_cases():
check.assert_equals(square_sum([1,2]), 5)
check.assert_equals(square_sum([0, 3, 4, 5]), 50)
check.assert_equals(square_sum([]), 0)
check.assert_equals(square_sum([-1,-2]), 5)
check.assert_equals(square_sum([-1,0,1]), 2)
``````
RELATED ARTICLES