CPSC 121 Fall 2012 Lecture Notes: Random Numbers and Sublist Sum, Study notes of Computer Science

These lecture notes from cpsc 121 (fall 2012) cover the topics of random numbers in python and writing a function to find the sum of all elements in sublists. Examples, hints, and solutions for exercises.

Typology: Study notes

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

105 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes CPSC 121 (Fall 2012)
Today ...
Random numbers
Homework
HW 7 due
HW 8 out
S. Bowers 1 of 3
pf3

Partial preview of the text

Download CPSC 121 Fall 2012 Lecture Notes: Random Numbers and Sublist Sum and more Study notes Computer Science in PDF only on Docsity!

Today ...

  • Random numbers

Homework

  • HW 7 due
  • HW 8 out

Exercise: More nested loop practice

Write a function called sublist sum(a list) that takes a list whose elements are lists of ints and returns the sum of all the values in each sublist. For example:

sublist_sum([[1,2], [2,3], [3,4]]) 15

sublist_sum([[1,2,3], [4,5,6]]) 21

Hint: Use a nested loop! Use either value loops or range loops

Answer:

def sublist_sum(a_list): total = 0 for sublist in a_list: for elem in sublist: total = total + elem return total