Introduction to Computer Science Programming with Python
Loops & Iteration Chapter 5 - Finding the Largest Value
Looping Through a Set
What is the Largest Number? Imagine we have a list of 1 billion numbers, and our task is to find the largest number How would you do this?
What is the Largest Number? Imagine we have a list of 1 billion numbers, and our task is to find the largest number How would you do this? Since computers can perform repetitive tasks in an extremely fast manner, the optimal and most efficient way is to let the computer find the largest number Write a program that loops through the list and find the largest number for loop Counter (in) And simply, a list of numbers Conditional statement to compare numbers
What is the Largest Number? In this example, we will not use 1 billion numbers so we can debug the code quicker. But, next, we will try this on larger scale Given list [9, 41, 12, 3, 74, 15] Initialize a variable named “largest_so_far” and initialize it with -1, which indicates I have not seen any numbers so far If the next number on the list is 9, and it is greater than -1, so, the largest so far is 9 However, we keep looping until the end of the list and whenever we find a greater number, we update the value of largest_so_far Lets code! Happy coding!
Finding the Largest value
How about the smallest Value? Tip, just a minor change
How about the smallest Value? Tip, just a minor change