Final answer:
The function max_int_in_list finds the largest integer in a list by iterating over each element, comparing it to the current maximum, and updating the maximum if necessary. It efficiently handles lists with all negative numbers by returning the least negative number.
Step-by-step explanation:
To write the function max_int_in_list that takes a list of integers and returns the biggest integer without using the built-in max function, you can start by assuming that the first number in the list is the largest. You can then iterate through the list, comparing each number with the current highest number, and updating it as needed when you find a bigger number. Here's one way to implement the function:
def max_int_in_list(lst):
max_int = lst[0]
for number in lst:
if number > max_int:
max_int = number
return max_int
# Example usage:
my_list = [5, 2, -5, 10, 23, -21]
biggest_int = max_int_in_list(my_list)
# biggest_int is now 23
This function works by setting max_int to the first value of lst and then comparing each subsequent integer with max_int, updating max_int if a larger integer is found. It successfully handles lists with all negative numbers as well, returning the least negative number (which is the maximum in a list of negative numbers).