Minimum time required to print given string from a circular container based on given conditions | Function to calculate minimum time to print all characters in the string ; Current element where the pointer is pointing ; Find index of that element ; Calculate absolute difference between pointer index and character index as clockwise distance ; Subtract clockwise time from 26 to get anti - clockwise time ; Add minimum of both times to the answer ; Add one unit time to print the character ; Print the final answer ; Driver code ; Given string word ; Function call
def minTime ( word ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE for i in range ( len ( word ) ) : NEW_LINE INDENT k = ord ( word [ i ] ) - 97 NEW_LINE a = abs ( curr - k ) NEW_LINE b = 26 - abs ( curr - k ) NEW_LINE ans += min ( a , b ) NEW_LINE ans += 1 NEW_LINE curr = ord ( word [ i ] ) - 97 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " zjpc " NEW_LINE minTime ( str ) NEW_LINE DEDENT