Weeks
Computational ThinkingWeek 8 · Overlaps, Sorted Lists and Side Effects
Week 8 Notes

Computational
Thinking 8

Finding the items two lists share, first by brute force and then with a cursor over sorted lists; what a side effect is and how a working copy avoids one; and deleting a dictionary key with and without a side effect.

6 chapters17 worked examples~18 min read
OverlapSorted listsSide effectsWorking copyDeleting a key

Chapters

  1. 01Finding Overlaps: the Brute-Force WayLists
  2. 02Speeding It Up With Sorted ListsLists
  3. 03What Is a Side Effect, Really?Lists
  4. 04Deleting a Key: on PurposeDictionaries
  5. 05Deleting a Key: Without a Side EffectDictionaries
  6. 06Week 8 Summary SheetAll topics
01
Lists

Finding Overlaps: the Brute-Force Way

FindOverlap compares every pair: simple, works in any order, costs n × m.

1.1The problem

Say you have two lists and want to know which items show up in both of them. The most straightforward way is to check every item in the first list against every single item in the second list. No assumptions, no shortcuts: just a full comparison.

FindOverlap.pseudo
Procedure FindOverlap(l1, l2)
  overlap = []
  foreach x in l1 {
    foreach y in l2 {
      if (x == y) {
        overlap = overlap ++ [x]
      }
    }
  }
  return(overlap)
End FindOverlap

For every x in l1, the inner loop walks through the entire l2 looking for a match. Whenever one is found, x is appended to overlap. This is the nested foreach from Week 5 (Maths and Physics toppers), now packaged as a procedure.

1.2Example

Examplel1 = [3, 7, 9], l2 = [7, 9, 12]. For 3: no match. For 7: matches 7. For 9: matches 9. Result: overlap = [7, 9].
l2 (inner: y)7912l1 (outer: x)3✗✗✗7✓✗✗9✗✓✗3 × 3 = 9 comparisonsoverlap[79]
Figure 1. All 9 pairs are compared; two of them match

1.3The cost

This works no matter how the lists are ordered, but that generality has a cost. If both lists have n items, you do roughly n × n comparisons in total (in general length(l1) × length(l2)). Fine for small lists, painfully slow for large ones: two lists of 1000 items need 1,000,000 comparisons.

1.4Going deeper

  • Order of the result (extra). Matches are appended in the order of l1.
  • Repeats (extra). Every matching pair is appended: FindOverlap([7], [7, 7]) gives [7, 7], and so does FindOverlap([7, 7], [7]).
  • No side effects. The procedure only reads l1 and l2 and builds a new list.

1.5Exam tips and common mistakes

Exam tips
  • Count comparisons as length(l1) × length(l2).
  • List matches in the order of l1.
Common mistakes
  • Stopping the inner loop at the first match: this version does not.
  • Thinking the order of the lists matters for correctness here: it does not.

Practice questions (graded-assignment style)

Q1Comparisons

Numerical Answer Type (NAT).

How many times is x == y checked when l1 has 4 items and l2 has 5?

Solution
Idea

Every pair.

  • 4 × 5 = 20.
  • Answer: 20
Q2Order of the result

Multiple Choice Question (MCQ): choose the one correct option.

What does FindOverlap([1, 2, 3], [3, 2]) return?

1
[3, 2]
2
[2, 3]
3
[1, 2, 3]
4
[]
Solution
Idea

Matches follow l1.

  • 2 is found before 3 because l1 is scanned in order.
  • Answer: option 2.
Q3Repeats

Numerical Answer Type (NAT).

What is the length of FindOverlap([7, 7], [7])?

Solution
Idea

Each x is checked against every y.

  • Both 7s of l1 match the 7 of l2: [7, 7].
  • Answer: 2
Q4Brute force

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements about FindOverlap.

1
It uses a foreach inside a foreach
2
It does length(l1) × length(l2) comparisons
3
It needs both lists to be sorted
4
It does not change l1 or l2
Solution
Idea

Think about order and cost.

  • 1, 2 and 4 are true. It works on unsorted lists too (statement 3 is false).
  • Answer: options 1, 2 and 4.
Revision overview
MethodEvery x against every y
Append on a match
Brute forceFindOverlap
Costn × m comparisons
1000 × 1000 = 1,000,000
Revision: overlap by comparing every pair
Next · ListsSpeeding It Up With Sorted Lists
02
Lists

Speeding It Up With Sorted Lists

FindOverlap2: a forward-only cursor over sorted lists, about n + m steps, with a catch.

2.1The idea: a cursor that only moves forward

If both lists are already sorted, we can be smarter. Once we have moved past a value in l2, we never need to look at it again: every later x in l1 is bigger. So instead of rescanning l2 from the top each time, we keep a cursor that only ever moves forward, the same trick used when merging two sorted lists.

Quick reminder. first(list) returns the head element. rest(list) returns everything after it: the list minus its head (Week 5).
FindOverlap2.pseudo
Procedure FindOverlap2(l1, l2)
  overlap = []
  if (length(l2) == 0) {
    return(overlap)
  }
  else {
    y = first(l2)
    l2 = rest(l2)
  }
  foreach x in l1 {
    while (y < x and length(l2) > 0) {
      y = first(l2)
      l2 = rest(l2)
    }
    if (x == y) {
      overlap = overlap ++ [x]
    }
  }
  return(overlap)
End FindOverlap2
  • If l2 is empty there is nothing to match, so the procedure returns [] at once.
  • Otherwise y holds the current item of l2 and l2 holds what is still ahead of it.
  • For each x, the while moves y forward while it is smaller than x (and items remain).
  • Then one check x == y decides whether x is in the overlap.

2.2Trace on the example

x moves through l1y only moves forward through l23797912x = 3: y stays 7x = 7: 7 == 7, matchx = 9: y moves to 9, matchnever looked at
Figure 2. y moves forward only: 7, then 9; 12 is never needed
xwhile loopyl2 leftx == y?overlap
3y = 7 is not < 3: no move7[9, 12]no[]
77 is not < 7: no move7[9, 12]yes[7]
97 < 9: move to 99[12]yes[7, 9]

A second run (illustrative): l1 = [2, 4, 6, 8, 10], l2 = [1, 4, 5, 8, 11, 12] gives [4, 8], the same as brute force.

2.3How much faster? (extra)

Each item of l2 is passed by the cursor at most once, and each x gets one equality check, so the work is about length(l1) + length(l2) instead of length(l1) × length(l2).

two sorted lists of 1000 items eachFindOverlap (every pair)1,000,000FindOverlap2 (cursor)2,999
Figure 3. Two sorted lists of 1000 random items each (counted by running both procedures)
How the tests were counted. For FindOverlap2, every check of the while condition and every x == y is counted. On the 3-item example that is 7 tests against brute force's 9; on 1000 items, 2,999 against 1,000,000. Both procedures found the same overlap.

2.4Only for sorted lists (extra)

The cursor relies on "every later x is bigger". On unsorted lists the procedure can miss matches: FindOverlap2([9, 7], [7, 9]) moves y past 7 while looking for 9, and then never comes back for 7. It returns [9]; the correct overlap is [9, 7].

Repeats (extra). With repeated values the two procedures can differ: FindOverlap([7], [7, 7]) gives [7, 7], but FindOverlap2([7], [7, 7]) gives [7], because each x is checked against one y only.

2.5The catch

CatchLook at the while loop: it reassigns l2 = rest(l2) directly on the parameter that was passed in, not on a copy. By the time the procedure returns, the caller's own list has been chopped down to whatever was left over, even though the caller never touched it. That is a side effect: the procedure silently changed something that lives outside itself.
caller-side.pseudo
l1 = [3, 7, 9]
l2 = [7, 9, 12]
common = FindOverlap2(l1, l2)
// common is [7, 9], but l2 is now [12]

2.6Exam tips and common mistakes

Exam tips
  • Trace with columns: x, the moves of y, what is left of l2, the check.
  • Always check the precondition: are both lists sorted?
Common mistakes
  • Using FindOverlap2 on unsorted lists.
  • Forgetting the empty-l2 case at the start.
  • Assuming the caller's l2 is unchanged afterwards.

Practice questions (graded-assignment style)

Q1A second run

Multiple Choice Question (MCQ): choose the one correct option.

With l1 = [2, 4, 6, 8, 10] and l2 = [1, 4, 5, 8, 11, 12], what does FindOverlap2 return?

1
[4]
2
[2, 4, 8]
3
[4, 8]
4
[8, 10]
Solution
Idea

Both lists are sorted.

  • 4 and 8 are in both lists.
  • Answer: option 3.
Q2The caller's list

Multiple Choice Question (MCQ): choose the one correct option.

After FindOverlap2([3, 7, 9], l2) with l2 = [7, 9, 12], what is the caller's l2?

1
[7, 9, 12]
2
[12]
3
[]
4
[9, 12]
Solution
Idea

Follow l2 = rest(l2).

  • 7 and 9 were taken off the front; only 12 is left.
  • Answer: option 2.
Q3Unsorted input

Multiple Choice Question (MCQ): choose the one correct option.

What does FindOverlap2([9, 7], [7, 9]) return?

1
[9]
2
[7, 9]
3
[9, 7]
4
[]
Solution
Idea

The cursor never goes back.

  • For x = 9 the cursor moves past 7 to 9 and matches; for x = 7 it stays at 9.
  • Answer: option 1.
Q4The sorted version

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements about FindOverlap2.

1
It needs both lists sorted
2
It does about length(l1) + length(l2) steps
3
The cursor y never moves backwards
4
It leaves the caller's l2 unchanged
Solution
Idea

Cost, precondition, side effect.

  • 1, 2 and 3 are true. It changes the caller's l2 (statement 4 is false).
  • Answer: options 1, 2 and 3.
Revision overview
Cursory = first(l2)
Moves while y < x
CostAbout n + m
2,999 vs 1,000,000
Sorted listsFindOverlap2
NeedsBoth lists sorted
Unsorted: misses matches
Catchl2 = rest(l2)
Caller's l2 shrinks
Revision: a forward-only cursor over two sorted lists
Next · ListsWhat Is a Side Effect, Really?
03
Lists

What Is a Side Effect, Really?

A change outside the procedure's own variables; FindOverlap3 avoids it with a working copy.

3.1Definition

Side effect

Any change a procedure makes to something outside its own local variables: mutating an input list, overwriting a global, printing to the screen. It is not automatically bad, but it turns dangerous the moment it is accidental: the caller has no way of knowing their data changed just from calling something that was supposed to only hand back an answer.

Think of it like thisIt is like asking a friend to check whether your fridge has milk, and they finish off the carton while looking. You asked for information; you did not sign up for a changed fridge.

3.2The fix: a working copy

Do not touch the parameter you were handed: make a working copy first, and let only the copy take the damage.

FindOverlap3.pseudo
Procedure FindOverlap3(l1, l2)
  overlap = []
  if (length(l2) == 0) {
    return(overlap)
  }
  else {
    myl2 = l2
    y = first(myl2)
    myl2 = rest(myl2)
  }
  foreach x in l1 {
    while (y < x and length(myl2) > 0) {
      y = first(myl2)
      myl2 = rest(myl2)
    }
    if (x == y) {
      overlap = overlap ++ [x]
    }
  }
  return(overlap)
End FindOverlap3

myl2 = l2 creates a separate working copy that the while loop is free to shrink with rest(), leaving the caller's original l2 completely untouched. Same result as before, minus the surprise.

caller-copy.pseudo
l1 = [3, 7, 9]
l2 = [7, 9, 12]
common = FindOverlap3(l1, l2)
// common is [7, 9], and l2 is still [7, 9, 12]
caller's l2 before the call[7912]after FindOverlap2[12]chopped down: side effectafter FindOverlap3[7912]untouched
Figure 4. The caller's l2 after FindOverlap2 and after FindOverlap3
FindOverlapFindOverlap2FindOverlap3
Needs sorted listsnoyesyes
Comparisonsabout n × mabout n + mabout n + m
Changes the caller's l2noyes (accidental)no

3.3Links to earlier weeks

  • Week 3 (side effects and contracts). A procedure's contract should list its side effects. FindOverlap2's contract would have to say "also empties your l2 up to the last item examined", which no caller wants.
  • Weeks 5 and 6 (tables). Procedures that move rows from Table 1 to Table 2 also change something outside themselves; that is why the table must be restored between scans.

3.4Pseudocode and Python (extra)

This course's pseudocode treats a list or dictionary parameter as the caller's own collection, so reassigning it inside the procedure changes what the caller sees, and myl2 = l2 makes a separate copy. Python, used in the other course, behaves differently:

  • l2 = l2[1:] inside a Python function only rebinds the local name; the caller's list is not changed.
  • In-place operations do leak: l2.pop(0), l2.append(x), l2.sort() or del d[k] change the caller's object.
  • myl2 = l2 makes a second name for the same list, not a copy; l2[:] or list(l2) copies it.

The habit is the same in both: know whether a call can change the data you passed in.

3.5Exam tips and common mistakes

Exam tips
  • For any procedure taking a list or dictionary, ask: is the parameter reassigned or changed?
  • Copy first, then shrink the copy.
Common mistakes
  • Calling a side effect "always a bug": it is only a problem when unexpected.
  • Shrinking the parameter itself for speed and forgetting the caller.

Practice questions (graded-assignment style)

Q1Spot the side effect

Multiple Choice Question (MCQ): choose the one correct option.

Which is a side effect of a procedure?

1
Creating a local variable count
2
Returning a value
3
Shrinking the list parameter it was given
4
Reading a list without changing it
Solution
Idea

Outside its own local variables.

  • Changing the list it was given affects the caller.
  • Answer: option 3.
Q2After FindOverlap3

Multiple Choice Question (MCQ): choose the one correct option.

After FindOverlap3([3, 7, 9], l2) with l2 = [7, 9, 12], what is the caller's l2?

1
[7, 9, 12]
2
[12]
3
[]
4
[9, 12]
Solution
Idea

Only the copy shrinks.

  • myl2 is shrunk; l2 is untouched.
  • Answer: option 1.
Q3Side effects

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
Printing to the screen is a side effect
2
FindOverlap3 returns the same overlap as FindOverlap2
3
Every side effect is a bug
4
A working copy keeps the caller's list unchanged
Solution
Idea

Accidental against intended.

  • 1, 2 and 4 are true. A side effect is not automatically a bug (statement 3 is false).
  • Answer: options 1, 2 and 4.
Revision overview
Side effectChange outside local variables
Dangerous when accidental
AnalogyCheck the milk
Carton finished
Side effectsFindOverlap3
Fixmyl2 = l2
Shrink only the copy
PythonRebinding: local only
pop, append, sort: leak
Revision: what a side effect is and how a working copy avoids one
Next · DictionariesDeleting a Key: on Purpose
04
Dictionaries

Deleting a Key: on Purpose

DeleteKey rebuilds the dictionary without k and assigns it back: an intended side effect.

4.1Deleting a key

The same idea shows up with dictionaries. Say you want to delete a key k from a dictionary d. One way: build a fresh dictionary, copy over every key–value pair except the one being deleted, then assign that fresh dictionary back onto d.

DeleteKey.pseudo
Procedure DeleteKey(d, k)
  myd = {}
  foreach key in keys(d) {
    if (k != key) {
      myd[key] = d[key]
    }
  }
  d = myd
End DeleteKey
d"Rahul"92"Clarence"73"Ritika"89k = "Clarence" is skippedmyd (copy all but k)"Rahul"92"Ritika"89d = myd"Rahul"92"Ritika"89
Figure 5. Copy every pair except k into myd, then d = myd
delete-call.pseudo
chemMarks = {"Rahul":92, "Clarence":73, "Ritika":89}
DeleteKey(chemMarks, "Clarence")
// chemMarks is now {"Rahul":92, "Ritika":89}

4.2This time it is intentional

IntendedChanging d here is not an accident: it is the whole point of the procedure. Side effects are not always bugs. Sometimes a procedure's entire job is to change something in place, the way deleting a dictionary entry or sorting a list in place naturally works. What matters is that the change is expected, not a surprise.

Compare this with FindOverlap2: there, the parameter got mutated as an accidental by-product of an optimisation. Here, mutating the parameter is the purpose; that is the entire reason the procedure exists.

4.3Going deeper

  • A key that is not there (extra). If k is not a key, every pair is copied and d ends up with the same contents: deleting a missing key is harmless.
  • The cost (extra). Deleting one key copies all the other pairs, so the work grows with the size of the dictionary.
  • Why build a new dictionary? The pseudocode has no "remove a key" operation, so the dictionary is rebuilt without it, just as Week 6 rebuilt a list around an inserted item.

4.4Exam tips and common mistakes

Exam tips
  • The test is k != key: copy when the keys differ.
Common mistakes
  • Writing k == key, which keeps only the key you meant to delete.
  • Calling an intended side effect a bug.

Practice questions (graded-assignment style)

Q1After DeleteKey

Multiple Choice Question (MCQ): choose the one correct option.

After DeleteKey(chemMarks, "Clarence") on {"Rahul":92, "Clarence":73, "Ritika":89}, what is chemMarks?

1
Unchanged
2
{"Rahul":92, "Ritika":89}
3
{"Clarence":73}
4
{}
Solution
Idea

Intended side effect.

  • The caller's dictionary loses "Clarence".
  • Answer: option 2.
Q2A missing key

Numerical Answer Type (NAT).

How many keys does chemMarks (3 keys) have after DeleteKey(chemMarks, "Asha")?

Solution
Idea

"Asha" is not a key.

  • Every pair is copied.
  • Answer: 3
Q3Wrong test

Multiple Choice Question (MCQ): choose the one correct option.

If the test were k == key instead of k != key, what would DeleteKey(chemMarks, "Clarence") leave?

1
{"Rahul":92, "Ritika":89}
2
Unchanged
3
{"Clarence":73}
4
{}
Solution
Idea

Which pairs are copied now?

  • Only the pair with key "Clarence" is copied.
  • Answer: option 3.
Revision overview
DeleteKeyCopy all pairs but k
d = myd
DeletingDeleteKey
IntendedThe change is the purpose
Not a surprise
Revision: deleting a key with an intended side effect
Next · DictionariesDeleting a Key: Without a Side Effect
05
Dictionaries

Deleting a Key: Without a Side Effect

DeleteKey2 returns the new dictionary; the caller assigns it, and stays in control.

5.1Hand back the result instead

If you would rather not rely on a side effect at all, write the procedure to simply hand back the new dictionary and let the caller decide what to do with it.

DeleteKey2.pseudo
Procedure DeleteKey2(d, k)
  myd = {}
  foreach key in keys(d) {
    if (k != key) {
      myd[key] = d[key]
    }
  }
  return(myd)
End DeleteKey2

myd = DeleteKey2(myd, k)

Notice the last line: the caller has to explicitly write myd = DeleteKey2(myd, k) to actually use the result. No hidden magic: if that reassignment is not written, nothing changes for the caller, exactly the way an ordinary function call should behave.

delete2-call.pseudo
chemMarks = {"Rahul":92, "Clarence":73, "Ritika":89}
DeleteKey2(chemMarks, "Clarence")
// result thrown away: chemMarks unchanged
chemMarks = DeleteKey2(chemMarks, "Clarence")
// now chemMarks is {"Rahul":92, "Ritika":89}

5.2The contrast

DeleteKey(d, k)changes the caller's d itselfintended side effectDeleteKey2(d, k)returns a new dictionaryd = DeleteKey2(d, k)the caller decidesno side effect
Figure 6. DeleteKey changes the caller; DeleteKey2 returns a result
DeleteKeyDeleteKey2
Ends withd = mydreturn(myd)
Caller writesDeleteKey(d, k)d = DeleteKey2(d, k)
Changes the caller's dautomaticallyonly if the caller assigns
Side effectyes, intendednone

DeleteKey mutates automatically and internally. DeleteKey2 stays "pure": it changes nothing on its own and puts the caller fully in control of whether and how the result is used. Week 6's InsertionSort is the same kind: mylist = InsertionSort(mylist).

5.3Exam tips and common mistakes

Exam tips
  • For a returning procedure, look for the assignment at the call site.
Common mistakes
  • Calling DeleteKey2 without assigning the result and expecting d to change.
  • Mixing the two: assigning the result of DeleteKey, which returns nothing.

Practice questions (graded-assignment style)

Q1Result thrown away

Multiple Choice Question (MCQ): choose the one correct option.

After the single statement DeleteKey2(chemMarks, "Clarence"), what is chemMarks?

1
Unchanged
2
Without "Clarence"
3
Empty
4
Only "Clarence"
Solution
Idea

No assignment at the call site.

  • The new dictionary is returned and thrown away; chemMarks is unchanged.
  • Answer: option 1.
Q2Using the result

Multiple Choice Question (MCQ): choose the one correct option.

With d the dictionary and k = "Clarence", which statement removes the key from d with DeleteKey2?

1
DeleteKey2(d, k)
2
k = DeleteKey2(d)
3
d = DeleteKey2(d, k)
4
DeleteKey2 = d
Solution
Idea

Assign the result.

  • The caller assigns the returned dictionary back.
  • Answer: option 3.
Q3Two styles

Multiple Select Question (MSQ): select all that apply.

Choose the correct statements.

1
DeleteKey2 has no side effect
2
With DeleteKey2 the caller decides whether d changes
3
DeleteKey returns the new dictionary
4
mylist = InsertionSort(mylist) follows the DeleteKey2 style
Solution
Idea

Who decides about the change?

  • 1, 2 and 4 are true. DeleteKey returns nothing useful (statement 3 is false).
  • Answer: options 1, 2 and 4.
Revision overview
DeleteKey2return(myd)
No side effect
Pure versionDeleteKey2
Callerd = DeleteKey2(d, k)
Without it: no change
Revision: returning a new dictionary instead of changing the old one
Next · All topicsWeek 8 Summary Sheet
06
All topics

Week 8 Summary Sheet

One page to revise all of Week 8: overlaps, sorted lists, side effects and deleting keys.

Key takeaways

The whole lecture boils down to one habit: knowing exactly when your code is quietly reaching outside itself.

  • Watch parameters closely. Reassigning a parameter inside a procedure (like l2 = rest(l2)) can quietly change the caller's copy too: that is a side effect, whether you meant it or not.
  • Avoid it with a local copy. If you do not want the caller's data touched, work on a copy (myl2 = l2) instead of the original parameter.
  • Sometimes it is the goal. Deleting a dictionary key or sorting a list in place are naturally in-place operations: the side effect there is intended, not a bug.
  • Or skip side effects entirely. Return a new collection and let the caller reassign it: myd = DeleteKey2(myd, k) or mylist = InsertionSort(mylist).
  • The real skill is not avoiding side effects altogether, but being deliberate about exactly when you use them.
Recap in one line

Every time a procedure takes a list or dictionary as input, ask yourself: am I about to change the caller's original, and do I actually want to?

Patterns and how to use them

Overlap by brute force
Every pair
pseudocode
foreach x in l1 {
  foreach y in l2 {
    if (x == y) {
      overlap = overlap ++ [x]
    }
  }
}

Use whenThe lists are unsorted, or small.

  1. Loop over l1.
  2. For each x, scan all of l2.
  3. Append x on every match.
Example[3, 7, 9] and [7, 9, 12]: 9 comparisons, [7, 9].
Overlap with a cursor
Sorted lists only
pseudocode
foreach x in l1 {
  while (y < x and length(myl2) > 0) {
    y = first(myl2)
    myl2 = rest(myl2)
  }
  if (x == y) {
    overlap = overlap ++ [x]
  }
}

Use whenBoth lists are sorted and large.

  1. Copy l2 to myl2 and take its first item as y.
  2. Move y forward while it is smaller than x.
  3. Check x == y once per x.
Example1000 + 1000 sorted items: 2,999 tests instead of 1,000,000.
Protect the caller with a copy
Working copy
pseudocode
myl2 = l2
y = first(myl2)
myl2 = rest(myl2)

Use whenA procedure needs to shrink or change a list it was given, but the caller should not notice.

  1. Copy the parameter into a local variable.
  2. Change only the copy.
  3. Leave the parameter alone.
ExampleAfter FindOverlap3 the caller's [7, 9, 12] is still [7, 9, 12].
Delete in place
Intended side effect
pseudocode
myd = {}
foreach key in keys(d) {
  if (k != key) {
    myd[key] = d[key]
  }
}
d = myd

Use whenChanging the caller's dictionary is the purpose of the procedure.

  1. Copy every pair except k.
  2. Assign the copy back to d.
  3. Document the side effect.
Example"Clarence" removed: {"Rahul":92, "Ritika":89}.
Return instead of change
Caller assigns
pseudocode
d = DeleteKey2(d, k)

Use whenYou want no side effects and the caller in control.

  1. Build and return a new collection.
  2. The caller assigns it back if wanted.
  3. Without the assignment nothing changes.
Examplemylist = InsertionSort(mylist) works the same way.

The procedures of this week

ProcedureNeedsCostSide effect
FindOverlapany listsn × mnone
FindOverlap2sorted listsabout n + mshrinks the caller's l2 (accidental)
FindOverlap3sorted listsabout n + mnone (works on a copy)
DeleteKeya dictionary, a keysize of dchanges d (intended)
DeleteKey2a dictionary, a keysize of dnone (returns a new dictionary)

Common mistakes

Cursor on unsorted lists
  • FindOverlap2([9, 7], [7, 9]) misses 7 and returns [9].
Shrinking the parameter
  • l2 = rest(l2) chops the caller's list; use myl2.
All side effects are bugs
  • Deleting a key in place is a side effect by design.
Ignoring the return value
  • DeleteKey2(d, k) alone changes nothing.
Repeats
  • With repeated values the brute-force and cursor versions can give different counts.
Python is different
  • Rebinding a parameter in Python is local; in-place methods are what leak.
Back to top