Skip to content

Commit 487ce96

Browse files
committed
differences for PR #1109
1 parent bdae797 commit 487ce96

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

10-defensive.md

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,11 @@ let's put them all in a function:
409409

410410
```python
411411
def test_range_overlap():
412-
assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
413-
assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
414412
assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
415413
assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
416414
assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
417-
assert range_overlap([]) == None
415+
assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
416+
assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
418417
```
419418

420419
We can now test `range_overlap` with a single function call:
@@ -426,20 +425,20 @@ test_range_overlap()
426425
```error
427426
---------------------------------------------------------------------------
428427
AssertionError Traceback (most recent call last)
429-
<ipython-input-29-cf9215c96457> in <module>()
428+
Cell , line 1
430429
----> 1 test_range_overlap()
431430
432-
<ipython-input-28-5d4cd6fd41d9> in test_range_overlap()
431+
Cell , line 3, in test_range_overlap()
433432
1 def test_range_overlap():
434-
----> 2 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
435-
3 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
436-
4 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
437-
5 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
433+
2 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
434+
----> 3 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
435+
4 assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
436+
5 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
438437
439-
AssertionError:
438+
AssertionError:
440439
```
441440

442-
The first test that was supposed to produce `None` fails,
441+
The test that was supposed to produce a simple range fails,
443442
so we know something is wrong with our function.
444443
We *don't* know whether the other tests passed or failed
445444
because Python halted the program as soon as it spotted the first error.
@@ -451,6 +450,65 @@ regardless of the input values.
451450
This violates another important rule of programming:
452451
*always initialize from data*.
453452

453+
We can update our function so that it initializes `max_left` and `min_right`
454+
from the input data itself.
455+
456+
```python
457+
def range_overlap(ranges):
458+
"""Return common overlap among a set of [left, right] ranges."""
459+
max_left, min_right = ranges[0]
460+
for (left, right) in ranges:
461+
max_left = max(max_left, left)
462+
min_right = min(min_right, right)
463+
return (max_left, min_right)
464+
```
465+
466+
Re-running our tests we see that we now pass the second test,
467+
but fail one of the non-overlapping cases.
468+
469+
```python
470+
test_range_overlap()
471+
```
472+
```error
473+
---------------------------------------------------------------------------
474+
AssertionError Traceback (most recent call last)
475+
Cell , line 1
476+
----> 1 test_range_overlap()
477+
478+
Cell , line 5, in test_range_overlap()
479+
3 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
480+
4 assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
481+
----> 5 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
482+
6 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
483+
484+
AssertionError:
485+
```
486+
It turns out that for our current implementation of the `range_overlap` function
487+
we never handled the case where there is no overlap. This shows up when
488+
`max_left` is either equal to or greater than `min_right`, which isn't an actual range.
489+
We can edit our function one last time to catch this behaviour and return `None`.
490+
491+
```python
492+
def range_overlap(ranges):
493+
"""Return common overlap among a set of [left, right] ranges."""
494+
max_left, min_right = ranges[0]
495+
for (left, right) in ranges:
496+
max_left = max(max_left, left)
497+
min_right = min(min_right, right)
498+
if max_left >= min_right:
499+
overlap = None
500+
else:
501+
overlap = (max_left, min_right)
502+
return overlap
503+
```
504+
505+
```python
506+
test_range_overlap()
507+
```
508+
509+
We get no output from `test_range_overlap()`,
510+
which means all the tests passed.
511+
454512
::::::::::::::::::::::::::::::::::::::: challenge
455513

456514
## Pre- and Post-Conditions

md5sum.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"episodes/07-cond.md" "e2e4b672a3cc52150cc74b4296f16531" "site/built/07-cond.md" "2023-11-17"
1313
"episodes/08-func.md" "99ce890dbe4944fd729665b566159d07" "site/built/08-func.md" "2025-05-22"
1414
"episodes/09-errors.md" "9eb44385622246a7b9e5e51ab0057c81" "site/built/09-errors.md" "2023-04-21"
15-
"episodes/10-defensive.md" "ec548cde2f4a5a3b7aa5301cc9bbc2f4" "site/built/10-defensive.md" "2023-11-16"
15+
"episodes/10-defensive.md" "511d79259815799fbd8cf65dc3e68258" "site/built/10-defensive.md" "2025-10-23"
1616
"episodes/11-debugging.md" "38db92997cae328f8e53a60735c46f97" "site/built/11-debugging.md" "2025-05-07"
1717
"episodes/12-cmdline.md" "6364192fd0440c6834d837055705d34b" "site/built/12-cmdline.md" "2023-11-10"
1818
"instructors/additional_material.md" "8032d13185179bda24bbc3d90ed22936" "site/built/additional_material.md" "2023-04-21"

0 commit comments

Comments
 (0)