Find Maximum Without Built-in Practice Problem
This data science coding problem helps you practice Control Flow & Loops, find maximum without built-in, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Control Flow & Loops.
- Problem ID: 77
- Problem key: 77-find-maximum-without-built-in
- URL: https://datacrack.app/solve/77-find-maximum-without-built-in
- Difficulty: easy
- Topic: Control Flow & Loops
- Module: Python Fundamentals
Problem Statement
# 🧩 Find Maximum Without Built-in
---
### 🎯 Goal
Finding the maximum (or minimum) of a sequence is a fundamental algorithm that teaches the **running best** pattern — a technique you'll use repeatedly in data science and ML.
---
### 🔍 The Running Best Pattern
The idea:
1. Assume the first element is the best so far.
2. Compare each subsequent element to the running best.
3. If the new element is better (larger), update the running best.
This same pattern underlies gradient descent in machine learning — continuously updating a "best" value as you process more information.
---
### 💻 Task
Implement `find_maximum(nums)` **without** using Python's built-in `max()` function.
---
### 📥 Input
- `nums`: list of numbers — guaranteed to be non-empty
### 📤 Output
- The maximum value in `nums`
---
### 🧩 Starter Code
```python
def find_maximum(nums):
"""
Find the largest element in a list without using max().
Args:
nums (list): Non-empty list of numbers
Returns:
number: The maximum value
"""
# 🧠 TODO: Initialize max_val with the first element
# 🧠 TODO: Loop through the rest, update max_val when you find something larger
pass
```
---
### 💡 Example
```python
find_maximum([3, 1, 4, 1, 5, 9, 2, 6])
# Expected: 9
find_maximum([-5, -1, -3])
# Expected: -1
```
---
### 🔑 Key Concepts
- Initialize with `nums[0]`, not `0` — otherwise negative numbers break it
- Use `nums[1:]` to iterate over remaining elementsStarter Code
def find_maximum(nums):
"""
Find the largest element in a list without using max().
Args:
nums (list): Non-empty list of numbers
Returns:
number: The maximum value
"""
# 🧠 TODO: Initialize max_val with the first element
# 🧠 TODO: Loop through the rest, update max_val when you find something larger
pass