forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41b7e25
commit 290a9d7
Showing
4 changed files
with
149 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,69 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
A module for summing the positive numbers in a list. | ||
Module contents: | ||
- sum_positive_numbers: calculates the sum of all positive numbers | ||
in a given list. | ||
Created on 2025-01-10 | ||
@author: Huda Alamassi | ||
""" | ||
|
||
|
||
def sum_positive_numbers(numbers: list) -> float: | ||
""" | ||
The function takes a list of numbers and returns the sum of all positive | ||
numbers in the list. Negative numbers and zeros are ignored. | ||
Arguments: | ||
numbers (list): A list of numbers (integers or floats) to be summed. | ||
- Assumed that the list contains numeric elements (integers or floats). | ||
Returns: | ||
float: | ||
- The sum of all positive numbers in the list. | ||
- If there are no positive numbers, it returns 0. | ||
Raises: | ||
AssertionError:If the input is not a list. | ||
AssertionError: If the list contains non-numeric elements. | ||
Examples: | ||
>>> sum_positive_numbers([1, 2, 3, -1, 0]) | ||
6 | ||
>>> sum_positive_numbers([-1, -2, -3]) | ||
0 | ||
>>> sum_positive_numbers([5, 0, 10.5, 2]) | ||
17.5 | ||
>>> sum_positive_numbers([0, 0, 0]) | ||
0 | ||
""" | ||
|
||
# Validate input type | ||
assert isinstance(numbers, list), "Input must be a list" | ||
|
||
for num in numbers: | ||
assert isinstance(num, (int, float)), ( | ||
f"List must contain only numeric elements, but found: {type(num)}" | ||
) | ||
|
||
# Ensures that if there's no data to process, we handle it efficiently | ||
if not numbers: | ||
return 0 | ||
|
||
total = 0 | ||
|
||
# Sum the positive numbers only | ||
for num in numbers: | ||
if num > 0: | ||
total += num | ||
|
||
return total |
Empty file.
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,80 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Test module for sum_positive_numbers function. | ||
Contains tests for summing positive numbers in a list, handling edge cases, and ensuring | ||
proper input validation. | ||
Test categories: | ||
- Functionality tests: checking if the function correctly sums positive numbers and | ||
ignores negatives or zeros | ||
- Edge cases: testing with empty lists, lists with only negative numbers or zeros, | ||
and lists with mixed data types | ||
- Defensive tests: incorrect input types such as non-list values or lists contains | ||
non-numeric data | ||
Created on 2025-01-10 | ||
Author: Huda Alamassi | ||
""" | ||
|
||
import unittest | ||
|
||
from solutions.sum_positive_numbers import sum_positive_numbers | ||
|
||
|
||
class TestSumPositiveNumbers(unittest.TestCase): | ||
"""Test the sum_positive_numbers function.""" | ||
|
||
def test_sum_with_positive_numbers(self): | ||
"""It should return the correct sum of positive numbers.""" | ||
actual = sum_positive_numbers([1, 2, 3]) | ||
self.assertEqual(actual, 6) | ||
|
||
def test_floats_and_integers(self): | ||
"""It should return the sum of both positive integers and floats properly.""" | ||
actual = sum_positive_numbers([1.5, 2, 3.5]) | ||
self.assertEqual(actual, 7.0) | ||
|
||
def test_sum_with_mixed_numbers(self): | ||
"""It should return the sum of the positive numbers and ignore negative numbers and zeros""" | ||
actual = sum_positive_numbers([5, 0, 10.5, -2, 3]) | ||
self.assertEqual(actual, 18.5) | ||
|
||
def test_single_positive_number(self): | ||
"""It should return the single positive number.""" | ||
actual = sum_positive_numbers([7]) | ||
self.assertEqual(actual, 7) | ||
|
||
def test_sum_with_no_positive_numbers(self): | ||
"""It should return 0 if there are no positive numbers.""" | ||
actual = sum_positive_numbers([-1, -2, -3]) | ||
self.assertEqual(actual, 0) | ||
|
||
def test_empty_list(self): | ||
"""It should return 0 if the list is empty.""" | ||
actual = sum_positive_numbers([]) | ||
self.assertEqual(actual, 0) | ||
|
||
def test_list_with_only_zeros(self): | ||
"""It should return 0 if the list contains only zeros.""" | ||
actual = sum_positive_numbers([0, 0, 0]) | ||
self.assertEqual(actual, 0) | ||
|
||
def test_single_negative_number(self): | ||
"""It should return 0 if the list contains only a single negative number.""" | ||
actual = sum_positive_numbers([-5]) | ||
self.assertEqual(actual, 0) | ||
|
||
# Test defensive assertions | ||
def test_defensive_assertion_for_non_list_input(self): | ||
"""Test that an assertion is raised if the input is not a list.""" | ||
with self.assertRaises(AssertionError): | ||
sum_positive_numbers(123) | ||
|
||
def test_defensive_assertion_for_non_numeric_elements(self): | ||
"""Test that an assertion is raised if the list contains non-numeric elements.""" | ||
with self.assertRaises(AssertionError): | ||
sum_positive_numbers([1, "a", 3]) # list contains a non-numeric element |
Empty file.