Day 1
Finding the minimum using a for loop
Problem
Given a non-empty list of numbers, find and print the smallest value by scanning the list once with a for loop (no built-in min).
Code
numbers = [4, 2, 9, 1, 7]
smallest = numbers[0]
for n in numbers:
if n < smallest:
smallest = n
print(smallest)
Description
The program starts by assuming the first element is the minimum, then walks the list. Whenever it sees a smaller number, it updates smallest. After the loop, that variable holds the minimum.