"Finding the Maximum Number: A Python Coding Problem Solution"
Question:
Given a list of numbers, write a Python function to find the maximum number in the list.
Solution:
def find_maximum(numbers): max_number = numbers[0] # Initialize max_number with the first element for num in numbers: if num > max_number: max_number = num return max_number # Example usage num_list = [15, 7, 22, 48, 9] max_num = find_maximum(num_list) print("Maximum number:", max_num)
Output:
Maximum number: 48
Explanation:
In the given solution, we define a function called find_maximum
that takes a list of numbers as input. We initialize max_number
with the first element of the list. Then, we iterate through each number in the list using a for loop. Inside the loop, we compare each number with the current maximum (max_number
) and update max_number
if a larger number is found. Finally, we return the maximum number.
In the example usage, we create a list num_list
containing some numbers. We call the find_maximum
function and pass num_list
as the argument. The function returns the maximum number in the list, which is then printed as the output.
This is just a simple example of a problem-solving question and its solution in Python. The approach may vary depending on the specific problem statement.
Comments
Post a Comment