Skip to content

Commit af472bc

Browse files
Merge pull request #439 from RocketPy-Team/mnt/find-closest-examples
MNT: adding example docs to the find_closest() function
2 parents 3f9271e + 509755d commit af472bc

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

rocketpy/tools.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import importlib, importlib.metadata
1+
import importlib
2+
import importlib.metadata
23
import re
34
from bisect import bisect_left
45

@@ -266,14 +267,29 @@ def find_closest(ordered_sequence, value):
266267
-------
267268
index : int
268269
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
270271
sequence, then 0 is returned. If the given value is greater than the
271272
last value in the sequence, then the index of the last value in the
272273
sequence is returned.
273-
"""
274-
if len(ordered_sequence) == 1:
275-
return 0
276274
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+
"""
277293
pivot_index = bisect_left(ordered_sequence, value)
278294
if pivot_index == 0:
279295
return pivot_index

0 commit comments

Comments
 (0)