Add same_value and last_same_value

This commit is contained in:
linarphy 2023-05-25 16:59:48 +02:00
parent ecc8ac804e
commit 3c0ece6688
No known key found for this signature in database
GPG key ID: 3D4AAAC3AD16E79C

View file

@ -206,6 +206,26 @@ def last_consecutive( list_ ):
else: # if inside the consecutive list and last element, every element is consecutive
break
return i
def same_value( list_ ):
"""
divide a sorted list of integer by same value part
"""
if not isinstance( list_ , list ) and not isinstance( list_ , np.ndarray ):
raise ValueError( 'list_ must be a list, ' + type( list_ ) + ' given' )
if len( list_ ) == 0:
return list_
index = last_same_value( list_ )
if index == len( list_ ) - 1:
return [ list_ ]
return same_value( list_[ : index + 1 ] ) + same_value( list_[ index + 1 : ] )
def last_same_value( list_ ):
"""
return the last index of the first same value list
"""
if not isinstance( list_ , list ) and not isinstance( list_ , np.ndarray ):
raise ValueError( 'list_ must be a list, ' + type( list_ ) + ' given' )
value = list_[0]
return np.argwhere( list_ == value ).max()
def rotate( image , angle ):
"""
rotate the following image by the given angle