Add functions

- Add consecutive
- Add last_consecutive
This commit is contained in:
linarphy 2023-05-09 12:16:21 +02:00
parent f75e2e2af9
commit 3bcc5b9f50
No known key found for this signature in database
GPG key ID: 3D4AAAC3AD16E79C

View file

@ -173,3 +173,32 @@ def find_point( list_ , index , axis = 'x' , threshold = 0.5 ):
inside = False
i += 1
return points
def consecutive( list_ ):
"""
divide a sorted list of integer by consecutive part
"""
if not isinstance( list_ , list ) and not isinstance( list_ , np.ndarray ):
raise ValueError( 'list_ must be a list, ' + type( list_ ) + ' given' )
index = last_consecutive( list_ )
if index == len( list_ ) - 1:
return [ list_ ]
return consecutive( list_[ : i + 1 ] ) + consecutive( list_[ i + 1 : ] ) # happy recursion \o/
def last_consecutive( list_ ):
"""
return the last index of the first consecutive list
"""
if not isinstance( list_ , list ) and not isinstance( list_ , np.ndarray ):
raise ValueError( 'list_ must be a list, ' + type( list_ ) + ' given' )
first , lower , greater = list_[0] , 0 , len( list_ )
while greater - lower != 1:
i = lower + ( greater - lower ) // 2
if list_[ i ] - first != i: # outside of the consecutive list
greater = i
else:
if i != len( list_ ) - 1:
if list_[ i ] + 1 != list_[ i + 1 ]: # next one is not inside the consecutive list => limit retrieved
break
lower = i
else: # if inside the consecutive list and last element, every element is consecutive
break
return i