Skip to content

Commit

Permalink
changed in lines 1,2
Browse files Browse the repository at this point in the history
  • Loading branch information
Muna-S committed Jan 10, 2025
2 parents 2bf39e3 + 6a4949e commit 157a3ee
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
58 changes: 58 additions & 0 deletions solutions/max_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for finding the maximum number of a list of numbers.
@author: Tagwa Hashim
"""


def max_in(numbers):
"""
Finds the maximum number in a list of numbers
Args:
numbers: A list of numbers (integers or floats).
Returns:
The maximum number in the list.
Raises:
ValueError: If the input is not a list.
ValueError: If the input list is empty.
TypeError: If the list contains non-numeric elements.
Examples:
>>> max_in([1, 5, 3, 9, 2])
9
>>> max_in([2.5, 1.02, 5.7])
5.7
>>> max_in([10.5, -5.2, 8])
10.5
>>> max_in([1, -5, -3])
1
>>> max_in([])
Traceback (most recent call last):
ValueError: List cannot be empty.
>>> max_in("not a list")
Traceback (most recent call last):
ValueError: Input must be a list.
>>> max_in([1, 2, "three"])
Traceback (most recent call last):
TypeError: List contains non-numeric elements.
"""
if not isinstance(numbers, list):
raise ValueError("Input must be a list.")
if not numbers:
raise ValueError("List cannot be empty.")
for num in numbers:
if not isinstance(num, (int, float)): # Check if any element is not numeric
raise TypeError("List contains non-numeric elements.")

max_num = numbers[0] # Initialize max_num with the first element

for num in numbers[1:]: # Iterate through the list starting from the second element
if num > max_num:
max_num = num

return max_num
57 changes: 57 additions & 0 deletions solutions/tests/test_max_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module: test_max_in
Description:
This module contains unit tests for the 'max_in' function defined
in the 'max_in.py' module.
Author: Tagwa Hashim
"""

import unittest
from ..max_in import max_in


class TestMaxIn(unittest.TestCase):
"""Test the max_in function"""

def test_positive_integers(self):
"""it should return 9 as the maximum number in this list"""

self.assertEqual(max_in([1, 5, 3, 9, 2]), 9)

def test_positive_floats(self):
"""it should return 10.5 as the maximum number in this list"""
self.assertEqual(max_in([10.5, 5.2, 8.7]), 10.5)

def test_mix_types(self):
"""it should return 1 as the maximum number in this list"""
self.assertEqual(max_in([1, -5.5, -3]), 1)

def test_empty_list(self):
"""It should raise AssertionError for empty list."""
with self.assertRaises(ValueError):
self.assertEqual(max_in([]), "List cannot be empty.")

def test_single_element_list(self):
"""It should return the single element."""
self.assertEqual(max_in([5]), 5)

def test_non_list_input(self):
"""It should raise AssertionError for non list input."""
with self.assertRaises(ValueError):
self.assertEqual(max_in("not a list"), "Input must be a list.")

def test_non_numeric_list(self):
"""It should raise AssertionError for non-numeric list elements."""
with self.assertRaises(TypeError):
self.assertEqual(
max_in([1, 2, "three"]), "List contains non-numeric elements."
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 157a3ee

Please sign in to comment.