human
What is a code to maximum time such that absolute difference between hour and minute lies in given range and the other steps required given the following:
Maximum time such that absolute difference between hour and minute lies in given range | Function checks whether given time is correct.
To check minute value of time.
To check hour value of time.
Changes in value is not allowed at position where ' ? ' is not present.
Function checks whether the absolute difference between hour and minute value is within [ L , R ].
Checks if the difference is outside the give range.
Displays time in proper 24 - hour format.
Function find the desired value of time whose difference lies in the range [ L , R ].
Decrease hour value from 23 to 0.
Check if the hour value is valid if not valid then no need to change minute value , since time will still remain in valid , to check hour value flag is set to 1..
Decrease minute value from 59 to 0.
Check if the minute value is valid , if not valid then skip the current iteration , to check ' minute ' value flag is set to 0..
Input time.
Difference range"
Solution in Python:
assistant
#Here is code to maximum time such that absolute difference between hour and minute lies in given range and the other steps required.
 def isValid ( a1 , a2 , strr , flag ) :
	v1 , v2 = 0 , 0
	if ( flag == 0 ) :
		v1 = strr [ 4 ]
		v2 = strr [ 3 ]
	else :
		v1 = strr [ 1 ]
		v2 = strr [ 0 ]
	if ( v1 != a1 and v1 != ' ? ' ) :
		return False
	if ( v2 != a2 and v2 != ' ? ' ) :
		return False
	return True
def inRange ( hh , mm , L , R ) :
	a = abs ( hh - mm )
	if ( a < L or a > R ) :
		return False
	return True
def displayTime ( hh , mm ) :
	if ( hh > 10 ) :
		print ( hh , end = " : " )
	elif ( hh < 10 ) :
		print ( "0" , hh , end = " : " )
	if ( mm > 10 ) :
		print ( mm )
	elif ( mm < 10 ) :
		print ( "0" , mm )
def maximumTimeWithDifferenceInRange ( strr , L , R ) :
	i , j = 0 , 0
	h1 , h2 , m1 , m2 = 0 , 0 , 0 , 0
	for i in range ( 23 , - 1 , - 1 ) :
		h1 = i % 10
		h2 = i // 10
		if ( not isValid ( chr ( h1 ) , chr ( h2 ) , strr , 1 ) ) :
			continue
		for j in range ( 59 , - 1 , - 1 ) :
			m1 = j % 10
			m2 = j // 10
			if ( not isValid ( chr ( m1 ) , chr ( m2 ) , strr , 0 ) ) :
				continue
			if ( inRange ( i , j , L , R ) ) :
				displayTime ( i , j )
				return
	if ( inRange ( i , j , L , R ) ) :
		displayTime ( i , j )
	else :
		print ( - 1 )
timeValue = " ? ? : ? ? "
L = 20
R = 39
maximumTimeWithDifferenceInRange ( timeValue , L , R )