|
1 | | -import importlib, importlib.metadata |
| 1 | +import importlib |
| 2 | +import importlib.metadata |
2 | 3 | import re |
3 | 4 | from bisect import bisect_left |
4 | 5 |
|
@@ -266,14 +267,29 @@ def find_closest(ordered_sequence, value): |
266 | 267 | ------- |
267 | 268 | index : int |
268 | 269 | The index of the closest value to the given value within the ordered |
269 | | - sequence. If the given value is less than the first value in the |
| 270 | + sequence. If the given value is lower than the first value in the |
270 | 271 | sequence, then 0 is returned. If the given value is greater than the |
271 | 272 | last value in the sequence, then the index of the last value in the |
272 | 273 | sequence is returned. |
273 | | - """ |
274 | | - if len(ordered_sequence) == 1: |
275 | | - return 0 |
276 | 274 |
|
| 275 | + Examples |
| 276 | + -------- |
| 277 | + >>> from rocketpy.tools import find_closest |
| 278 | + >>> find_closest([1, 2, 3, 4, 5], 0) |
| 279 | + 0 |
| 280 | + >>> find_closest([1, 2, 3, 4, 5], 1.5) |
| 281 | + 0 |
| 282 | + >>> find_closest([1, 2, 3, 4, 5], 2.0) |
| 283 | + 1 |
| 284 | + >>> find_closest([1, 2, 3, 4, 5], 2.8) |
| 285 | + 2 |
| 286 | + >>> find_closest([1, 2, 3, 4, 5], 4.9) |
| 287 | + 4 |
| 288 | + >>> find_closest([1, 2, 3, 4, 5], 5.5) |
| 289 | + 4 |
| 290 | + >>> find_closest([], 10) |
| 291 | + 0 |
| 292 | + """ |
277 | 293 | pivot_index = bisect_left(ordered_sequence, value) |
278 | 294 | if pivot_index == 0: |
279 | 295 | return pivot_index |
|
0 commit comments