← Back to challenges

Sum of Numbers in a List

PythonHardarrayslanguage_fundamentalsnumbersmath

Instructions

Create a function that takes a list of numbers lst as an argument. Square each number in the list if the number is even and square root the number if it is odd. Return the sum of the new list rounded to two decimal places.

Example:

[2, 4, 9]  ➞ 23
2 ^ 2 + 4 ^ 2 + √9 = 4 + 16 + 3 = 23

Examples

list_sum([1, 3, 3, 1, 10]) ➞ 105.46

list_sum([2, 3, 4, 5]) ➞ 23.97

list_sum([1, 31, 3, 11, 0]) ➞ 11.62

Notes

  • No empty list in Tests.
  • Each list element ≥ 0.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Stripping a Sentence Down