Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
def findSegment ( n , m , segment_length ) : NEW_LINE INDENT meet_point = ( 1.0 * n ) / 2.0 NEW_LINE sum = 0 NEW_LINE segment_number = 0 NEW_LINE for i in range ( 0 , m , 1 ) : NEW_LINE INDENT sum += segment_length [ i ] NEW_LINE if ( sum == meet_point ) : NEW_LINE INDENT segment_number = - 1 NEW_LINE break NEW_LINE DEDENT if ( sum > meet_point ) : NEW_LINE INDENT segment_number = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT return segment_number NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE m = 3 NEW_LINE segment_length = [ 3 , 2 , 8 ] NEW_LINE ans = findSegment ( n , m , segment_length ) NEW_LINE print ( ans ) NEW_LINE DEDENT