Use the "Check" button only for the Parsons Puzzles. For the survey questions below them, your responses are saved automatically when you select themβno need to click any buttons.
Subsection4.1Puzzle 1: Calculate Sum of Positive Numbers
Question4.1.1.
Arrange the code blocks to create a function called sum_positive that takes a list of numbers and returns the sum of only the positive numbers (numbers greater than 0).
def sum_positive(numbers):
---
total = 0
---
for num in numbers:
---
if num > 0:
---
if num >= 0: #paired
---
total = total + num
---
total = num #paired
---
return total
def find_max(numbers):
---
max_val = numbers[0]
---
max_val = 0 #paired
---
for num in numbers:
---
if num > max_val:
---
if num < max_val: #paired
---
max_val = num
---
return max_val
Arrange the code blocks to create a function called count_matches that takes a list of numbers and a target value, and returns how many times the target appears in the list.
def count_matches(numbers, target):
---
count = 0
---
for num in numbers:
---
if num == target:
---
if num != target: #paired
---
count = count + 1
---
count = count + num #paired
---
return count
Arrange the code blocks to create a function called find_first that takes a list of numbers and a target value, and returns True if the target is found in the list, or False if it is not found.
def find_first(numbers, target):
---
for num in numbers:
---
if num == target:
---
if num != target: #paired
---
return True
---
return False #paired
---
return False