Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
Big Idea 3.10 Part 4- Pseudocode
Learn more about lists
Find the minimum value in a list
- Create or access the list.
- Make a variable to hold the minimum and set it to a potential minimum value.
- Loop through the list.
- Check each element to see if it is less than the minimum variable.
- If the element is less than the minimum variable, update the minimum.
- After all elements of the list have been checked, display the minimum value.
Pseudocode
nums ← [65, 89, 92, 35, 84, 78, 28, 75]
min ← nums [1]
FOR EACH score IN nums:
{
IF(score < min)
{
min = score
}
}
DISPLAY (min)
Sum of Even Numbers of a list
# Python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_sum = 0
for score in nums:
if score % 2 == 0: # Check if the remainder when divided by 2 is 0 (even number)
even_sum += score # If previous requirement is fulfilled, add to sum
print("Sum of even numbers in the list:", even_sum)
%%js
// Javascript
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let even_sum = 0;
for (let score of nums) {
if (score % 2 === 0) {
even_sum += score; // Add score to even_sum if it's even
}
}
console.log("Sum of even numbers in the list:", even_sum);
Homework
- Find the sum of even numbers in a list
- Find the minimum and maximum values in a list