Problem Solving — CS & DS

Lecture 11 — Lists and Slicing

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The queue reorder: rotations as slices

cs-041

Queue [A, B, C, D, E], k = 2 (last 2 to front). What do k = 0, 5, 7 give?

Expected: 0 unchanged | 2 [D,E,A,B,C] | 5 unchanged | 7 [D,E,A,B,C]

Cost: O(n); the in-place three-reversal rotation (O(1) extra space) is the enrichment extension.

Worked example

Deduplication patterns: order vs sorted

cs-042

[3, 1, 3, 2, 1, 3] → order-preserving dedupe, and the sorted-unique variant. Reference: [3, 1, 2] and [1, 2, 3].

Expected: [3, 1, 2] [1, 2, 3]

Cost: O(n) with a seen-set; the naive 'not in keep' is O(n^2) - measurable at n = 10,000.

Worked example

Rotate and roll: left, right, and wraparound

cs-043

[1, 2, 3, 4, 5]: rotate left by 2 → [3,4,5,1,2]; right by 2 → [4,5,1,2,3].

Expected: [3, 4, 5, 1, 2] | [4, 5, 1, 2, 3]

Cost: O(n); in-place three-reversal is O(n) time, O(1) extra space.

Worked example

The aliasing trap: [[0]*3]*2

cs-044

grid = [[0]*3]*2; grid[0][0] = 9. What does grid hold? Then the correct construction - and the same assignment there?

a = [1, 2]
b = a
b.append(3)
print(a, b is a)

Expected: [[9, 0, 0], [9, 0, 0]] then [[9, 0, 0], [0, 0, 0]]

Cost: Constant per assignment; the defect class returns as cs-047's swap and cs-027's ghost duplicate.

Case attempt · lecture 11
Case cs-041 · Beginner

The Queue Reorder

Move the last k people of a queue to the front, preserving order. Slicing plus concatenation; discuss negative indices.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-041 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-041 — not embedded in this deck.

Case attempt · lecture 11
Case cs-042 · Foundational

Deduplication Patterns

Remove duplicates three ways: preserving order with a seen-set, via sorting, and the naive quadratic way. Compare and test all three.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-042 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-042 — not embedded in this deck.

Case attempt · lecture 11
Case cs-043 · Intermediate

Rotate and Roll

Rotate a list by k positions (left or right) without extra library calls. Handle k larger than the length; connect rotation to slicing.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-043 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-043 — not embedded in this deck.

Case attempt · lecture 11
Case cs-044 · Advanced

The Aliasing Trap

A matrix is built as [[0]*3]*2 and rows mutate together. Explain why, then build it correctly. Aliasing as the deepest beginner trap.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-044 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-044 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What is the difference between a = b and a = b[:] for lists?
  2. Give the slice for the last three items and for every second item.
  3. Why did the roster corrupt in cs-044?
Expected answers (instructor): the first aliases (one object, two names), the second copies lst[-3:] and lst[::2] two names referenced one list; an 'append' to one mutated the other
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF