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.
Revert "Delete solutions/largest_number.py"
This reverts commit 4307189.
- Loading branch information
1 parent
c641786
commit 66d27ff
Showing
1 changed file
with
62 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,62 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for finding the largest number. | ||
Module contents: | ||
- largest_number: Extracts the largest number from a list of integers. | ||
Created on 11-01-2025 | ||
@author: Azza | ||
""" | ||
|
||
|
||
def largest_number(list_numbers: list) -> int: | ||
"""Extract the largest number from the given list of integers. | ||
Args: | ||
list_numbers (list): A list of integers. | ||
Returns: | ||
int: The largest number in the list. | ||
Raises: | ||
TypeError: If the input is not a list or contains non-integer elements. | ||
ValueError: If the input list is empty. | ||
Examples: | ||
>>> largest_number([0, 1, 9, 2, 6, 4]) | ||
9 | ||
>>> largest_number([12]) | ||
12 | ||
>>> largest_number([8, -1, -8, 5]) | ||
8 | ||
>>> largest_number([-9, -1, -6, -5]) | ||
-1 | ||
>>> largest_number([0, 0]) | ||
0 | ||
>>> largest_number([]) # Raises ValueError | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The list must not be empty. | ||
>>> largest_number(["-5", 5, "0"]) # Raises TypeError | ||
Traceback (most recent call last): | ||
... | ||
TypeError: All elements in the list must be integers. | ||
>>> largest_number(42) # Raises TypeError | ||
Traceback (most recent call last): | ||
... | ||
TypeError: The input must be a list. | ||
>>> largest_number([2**32, 2**16, 2**8, 1]) | ||
4294967296 | ||
""" | ||
|
||
# Ensure the input is a list of integers | ||
if not isinstance(list_numbers, list): | ||
raise TypeError("The input must be a list.") | ||
if not list_numbers: | ||
raise ValueError("The list must not be empty.") | ||
if not all(isinstance(i, int) for i in list_numbers): | ||
raise TypeError("All elements in the list must be integers.") | ||
|
||
return max(list_numbers) |