COSI 2a Lecture 5: COSI Week 5

36 views2 pages

Document Summary

Summing the elements of a list def add_elements(vals): total = 0 for x in vals: total = total + x return total. Write a function count_as(scores) which returns the number of a"s in a list of test scores. So, count_as([90,85,95,100,75,91]) returns 4 and count_as([89,88,65,84]) returns 0. Solution: def count_as(scores): total=0 for x in scores: if x>89: total=total+1 return total. Write a function sum_pos(vals) which returns the sum of all the positive values in vals. So, sum_pos([5,-2,3,-4]) will return 8 sum_pos([-1,-2,-3]) will return 0. Solution: def sum_pos(vals): total=0 for x in vals: if x>0: total=total+x return total. Write a function, count_odds(vals), which returns the number of values v in vals which are odd count_odds([1,2,3,4,5]) --> 3 count_odds([2,4,6,8]) --> 0 count_odds([1,3,5,2,4,6,7]) --> 4. Solution: def count_odds(vals): total=0 for x in vals: if (x%2==1): total=total+1 return total. Write a function, sum_odds(vals), which returns the sum of the odd numbers in vals sum_odds([1,2,3,4,5]) --> 9 sum_odds([2,4,6,8]) --> 0 sum_odds([1,3,5,2,4,6,7]) --> 16.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions