Skip to content

Commit

Permalink
Merge pull request #119 from MIT-Emerging-Talent/find_largest
Browse files Browse the repository at this point in the history
solution #3: find the largest in a list
  • Loading branch information
yuri-spizhovyi-mit authored Jan 12, 2025
2 parents 9896379 + 5891077 commit 9a63ff7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
34 changes: 34 additions & 0 deletions solutions/find_largest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Finds the Largest Number in a List
This file contains a function to find the largest number in a given list of numbers.
Created on 01/10/2025
Author: Khadija Al Ramlawi
"""


def find_largest(numbers: list) -> int:
"""
Finds the largest number in the given list.
Parameters:
numbers (list): A list of numeric values.
Returns:
int: The largest number in the list.
Raises:
AssertionError: If the input is not a list.
AssertionError: If the input list is empty.
AssertionError: If the input contains non-numeric values.
Examples:
>>> find_largest([1, 2, 3])
3
>>> find_largest([10, 20, 30, 40])
40
>>> find_largest([-5, -1, -10])
-1
"""
assert isinstance(numbers, list), "Input must be a list."
assert len(numbers) > 0, "List cannot be empty."
assert all(isinstance(num, (int, float)) for num in numbers), (
"All elements must be numeric."
)

return max(numbers)
54 changes: 54 additions & 0 deletions solutions/tests/test_find_largest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Unit test for Find Largest Number Function
Created on 01/10/2025
Author : Khadija Al Ramlawi
"""

import unittest
from solutions.find_largest import find_largest


class TestFindLargest(unittest.TestCase):
"""Test suite for for find_largest function."""

def test_positive_numbers(self):
"""List with positive numbers"""
self.assertEqual(find_largest([1, 2, 3]), 3)

def test_mixed_numbers(self):
"""List with both positive and negative numbers"""
self.assertEqual(find_largest([-10, 0, 5, 20]), 20)

def test_all_negative_numbers(self):
"""List with all negative numbers"""
self.assertEqual(find_largest([-5, -1, -10]), -1)

def test_single_element(self):
"""List with a single element"""
self.assertEqual(find_largest([42]), 42)

def test_large_numbers(self):
"""List with large numbers"""
self.assertEqual(find_largest([1000000, 5000000, 2000000]), 5000000)

def test_empty_list(self):
"""Empty list should raise an AssertionError"""
with self.assertRaises(AssertionError):
find_largest([])

def test_non_numeric_values(self):
"""List with non-numeric values should raise an AssertionError"""
with self.assertRaises(AssertionError):
find_largest([1, 2, "three"])

def test_invalid_input_non_list(self):
"""Non-list input should raise an AssertionError"""
with self.assertRaises(AssertionError):
find_largest(123)

def test_invalid_input_none(self):
"""None as input should raise an AssertionError"""
with self.assertRaises(AssertionError):
find_largest(None)

0 comments on commit 9a63ff7

Please sign in to comment.