forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sum of the squares of the numbers in a list
- Loading branch information
1 parent
1ee2826
commit faa3caf
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Created 01-11-2025 | ||
@Author: Mithchell Cenatus | ||
This module provides a function to calculate the sum of the squares of the numbers in a list. | ||
""" | ||
|
||
from typing import Union | ||
|
||
|
||
def sum_of_square(numbers: list[Union[int, float]]) -> Union[int, float, None]: | ||
""" | ||
Calculates the sum of the squares of the numbers in a list. | ||
Parameters: | ||
numbers (list[int | float]): The input list of numbers. | ||
Returns: | ||
int | float: The sum of the squares of the numbers in the provided list. | ||
None: If the input list is invalid. | ||
Raises: | ||
TypeError: If the input is not a list or contains invalid elements. | ||
Examples: | ||
>>> sum_of_square([1, 2, 3]) | ||
14 | ||
>>> sum_of_square([2.5, 1.1, 4]) | ||
23.46 | ||
>>> sum_of_square([-5.3, 9, -11, 2]) | ||
23.09 | ||
>>> sum_of_square([]) | ||
0 | ||
""" | ||
if not isinstance(numbers, list): # Ensure the input is a list | ||
raise TypeError("Input must be a list of integers and/or floats.") | ||
|
||
if not all( | ||
isinstance(num, (int, float)) for num in numbers | ||
): # Check if all elements are integers or floats | ||
raise TypeError("All elements in the list must be integers or floats.") | ||
|
||
if not numbers: # Handle an empty list | ||
return 0 | ||
|
||
# Calculate the sum of squares | ||
squares = (num**2 for num in numbers) # Generator expression for efficiency | ||
total = sum(squares) # Sum up all squared values | ||
return total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created 01-11-2025 | ||
@author: Mithchell Cenatus | ||
This class contains unit tests for the sum_of_square function. | ||
The function gets a list of integers and/or floats as input and returns the sum of the squares of its elements. | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.sum_of_squares import sum_of_square | ||
|
||
|
||
class TestSumOfSquare(unittest.TestCase): | ||
""" | ||
Unit tests for the sum_of_square function. | ||
""" | ||
|
||
def test_valid_list(self): | ||
"""It should return 55 for [1, 2, 3, 4, 5].""" | ||
self.assertEqual(sum_of_square([1, 2, 3, 4, 5]), 55) | ||
|
||
def test_negative_valid_list(self): | ||
"""It should return 102 for [-1, -2, 4, 9].""" | ||
self.assertEqual(sum_of_square([-1, -2, 4, 9]), 102) | ||
|
||
def test_mixed_numbers(self): | ||
"""It should return 234.09 for [-5.3, 9, -11, 2].""" | ||
self.assertAlmostEqual(sum_of_square([-5.3, 9, -11, 2]), 234.09, places=2) | ||
|
||
def test_empty_list(self): | ||
"""It should return 0 for an empty list.""" | ||
self.assertEqual(sum_of_square([]), 0) | ||
|
||
def test_non_list_input(self): | ||
"""Test that a TypeError is raised when input is not a list.""" | ||
with self.assertRaises(TypeError): | ||
sum_of_square("not a list") | ||
|
||
def test_invalid_elements(self): | ||
"""Test that a TypeError is raised when list contains invalid elements.""" | ||
with self.assertRaises(TypeError): | ||
sum_of_square([20, 25, -1, "8"]) | ||
|
||
def test_large_numbers(self): | ||
"""It should return 2_000_000_000_001 for [1_000_000, 1, 0, -1_000_000].""" | ||
self.assertEqual( | ||
sum_of_square([1_000_000, 1, 0, -1_000_000]), 2_000_000_000_001 | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |