1 of 17

CSE 160 Section 6

Course Checkpoint & Practice

CSE 160: Course Checkpoint

2 of 17

Yay! We’ve made it past the halfway mark of the quarter 🙌

Give yourself a pat on the back!

CSE 160: Course Checkpoint

3 of 17

Logistics

  • Section Assignment 5 due Friday, May 8th @ 11:59 PM
  • Programming Practice 6 due Tuesday, May 12th @ 11:59 PM
  • Clustering HW due Friday, May 15th @ 11:59 PM
    • Submitting on Gradescope
    • Wait for autograder!
  • Resubmission Cycle 5/4 – 5/8
    • Don’t forget to fill out the resubmission form!

CSE 160: Course Checkpoint

4 of 17

Quest Reflection: �What problem did you find most challenging? Other thoughts about the Quest?

CSE 160: Course Checkpoint

5 of 17

Section Handout Problems

CSE 160: Course Checkpoint

6 of 17

Problem 1.1

Trace the following code. What will lst2 and lst3 evaluate to at the end of the execution?

def change_val(lst):

lst[0] = 13

def append_val(lst):

lst.append(99)

def mystery(lst):

lst = [78, 24]

return lst

lst2 = [1, 2]

change_val(lst2)

append_val(lst2)

lst3 = mystery(lst2)

CSE 160: Course Checkpoint

7 of 17

Problem 1.1

lst2: [13, 2, 99]

lst3: [78, 24]

CSE 160: Course Checkpoint

8 of 17

Problem 1.2

What does the following code output?

a = [["x"]]

b = [["y"]]

temp = a[0]

a[0] = b[0]

b[0] = temp

print(b + [a[0][0]])

Python Tutor

CSE 160: Course Checkpoint

9 of 17

Problem 1.2

[['x'], 'y'] list

CSE 160: Course Checkpoint

10 of 17

Problem 2

You are given two files, words.txt and targets.txt.

Write a function count_targets that takes two filenames, words_file and targets_file, and returns a dictionary mapping each target word to the number of times it appears in words_file.

If a target word does not appear, its count should be 0.

Example:

count_targets("words.txt", "targets.txt")

returns {"apple": 3, "orange": 1, "grape": 0}

Buggy code:

def count_targets(words_file, targets_file):

words = open(words_file)

targets = open(targets_file)

result = {}

for target in targets:

result[target] = 0

for line in words.read():

word = line.strip()

result[word] += 1

words.close()

targets.close()

return result

CSE 160: Course Checkpoint

11 of 17

Problem 2

def count_targets(words_file, targets_file):

words = open(words_file)

targets = open(targets_file)

result = {}

for line in targets:

target = line.strip()

result[target] = 0

for line in words:

word = line.strip()

if word in result:

result[word] += 1

words.close()

targets.close()

return result

  • Need to strip each target before using it as a dictionary key.

  • Need to loop over words directly, not words.read(), because words.read() loops through characters.

  • Need to only count words that are target words.

  • Need to check if word is in result before doing result[word] += 1.

CSE 160: Course Checkpoint

12 of 17

Problem 3

Write a function find_first_below that takes a 1-dimensional list of integers and a number limit, and returns the index of the first value in the list that is less than limit.

If no value is less than limit, return -1.

Examples:

find_first_below([8, 6, 3, 7], 5) returns 2

find_first_below([10, 12, 15], 5) returns -1

find_first_below([4, 2, 9], 5) returns 0

CSE 160: Course Checkpoint

13 of 17

Problem 3

def find_first_below(lst, limit):

for i in range(len(lst)):

if lst[i] < limit:

return i

return -1

CSE 160: Course Checkpoint

14 of 17

Problem 4

A hiking app stores daily step counts in a nested list. Each inner list represents one week.

steps = [

[9000, 8500, 7200],

[10000, 6000, 5000],

[4000, 11000, 3000]

]

Write a function find_first_low_day that takes a nested list of integers weeks and a number limit, and returns a 2-element list [week_index, day_index] representing the first day where the step count is less than limit.

Search through the nested list row by row, from left to right. Call find_first_below in your solution.

If no day has fewer than limit steps, return [-1, -1].

Examples:

find_first_low_day(steps, 8000) returns [0, 2]

find_first_low_day(steps, 5500) returns [1, 2]

find_first_low_day(steps, 2500) returns [-1, -1]

CSE 160: Course Checkpoint

15 of 17

Problem 4

def find_first_low_day(weeks, limit):

for r in range(len(weeks)):

c = find_first_below(weeks[r], limit)

if c != -1:

return [r, c]

return [-1, -1]

CSE 160: Course Checkpoint

16 of 17

Section Code: xx

CSE 160: Course Checkpoint

17 of 17

Looking Ahead: �CSVs and Objects

Be sure to remember the section code!

CSE 160: Course Checkpoint