Commit 0b32b10
committed
types(intro_sort): constrain items to Comparable
Bind every function in sorts/intro_sort.py to a Comparable Protocol so the
signatures say "a list of items that can be compared with each other"
instead of a bare list, and keep the element type in the return.
Two of the hints were outright wrong rather than merely loose:
- median_of_3 returned int but returns an element of the collection, so it
is now T
- partition took pivot: int but takes an element of the collection, so it is
now T
Both were only reachable with the wrong type through untyped callers.
Doctests added for a comparable non-int type (strings) and for the failure
mode on insertion_sort, heap_sort, median_of_3, partition and sort: mixing
non-comparable items must raise TypeError rather than silently mis-sort.
intro_sort had no test at all. tests/test_sorts.py now adds it to the shared
battery and to the rejection check, plus a dedicated test that reaches the
branches the battery cannot: every shared case is shorter than the
16-element threshold, so the battery only ever exercises insertion_sort.
The new test sorts 17/32/100/500-element int and str inputs to take the
quicksort branch, and drives intro_sort with a depth budget of 0 to take the
heapsort branch. The RNG is seeded so the test is deterministic.1 parent db6bf8a commit 0b32b10
2 files changed
Lines changed: 86 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
5 | 11 | | |
6 | 12 | | |
7 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
8 | 19 | | |
9 | 20 | | |
10 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
11 | 24 | | |
12 | 25 | | |
13 | 26 | | |
| |||
24 | 37 | | |
25 | 38 | | |
26 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
27 | 44 | | |
28 | 45 | | |
29 | 46 | | |
| |||
36 | 53 | | |
37 | 54 | | |
38 | 55 | | |
39 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
40 | 59 | | |
41 | 60 | | |
42 | 61 | | |
| |||
56 | 75 | | |
57 | 76 | | |
58 | 77 | | |
59 | | - | |
| 78 | + | |
60 | 79 | | |
61 | 80 | | |
62 | 81 | | |
| |||
66 | 85 | | |
67 | 86 | | |
68 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
69 | 94 | | |
70 | 95 | | |
71 | 96 | | |
| |||
79 | 104 | | |
80 | 105 | | |
81 | 106 | | |
82 | | - | |
83 | | - | |
84 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
85 | 110 | | |
86 | 111 | | |
87 | 112 | | |
| |||
92 | 117 | | |
93 | 118 | | |
94 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
95 | 126 | | |
96 | 127 | | |
97 | 128 | | |
| |||
105 | 136 | | |
106 | 137 | | |
107 | 138 | | |
108 | | - | |
| 139 | + | |
109 | 140 | | |
110 | 141 | | |
111 | 142 | | |
| |||
119 | 150 | | |
120 | 151 | | |
121 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
122 | 157 | | |
123 | 158 | | |
124 | 159 | | |
| |||
134 | 169 | | |
135 | 170 | | |
136 | 171 | | |
137 | | - | |
| 172 | + | |
138 | 173 | | |
139 | 174 | | |
140 | 175 | | |
| |||
155 | 190 | | |
156 | 191 | | |
157 | 192 | | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
158 | 199 | | |
159 | 200 | | |
160 | 201 | | |
| |||
163 | 204 | | |
164 | 205 | | |
165 | 206 | | |
166 | | - | |
167 | | - | |
168 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
169 | 210 | | |
170 | 211 | | |
171 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
62 | 94 | | |
63 | 95 | | |
64 | 96 | | |
| |||
83 | 115 | | |
84 | 116 | | |
85 | 117 | | |
| 118 | + | |
86 | 119 | | |
87 | 120 | | |
88 | 121 | | |
| |||
152 | 185 | | |
153 | 186 | | |
154 | 187 | | |
| 188 | + | |
155 | 189 | | |
156 | 190 | | |
157 | 191 | | |
| |||
0 commit comments