# Answer to Question #4550 in Python for Kyle

Question #4550

I need to make a program that when asked the number of minutes you sleep. ie 437. It will tell you 17:00 to 00:17, 18:01 to 01:18. Basically the times when the first and last digits are backwards and 437 minutes apart. I am having a hard time setting up the loops and if statements. How to I go about this? How many while or for loops will I need? Please help.

Expert's answer

# the function counts the number of minutes between two times

# Input:

# t1, t2 - strings, represeting time in the form

"HH:MM",

# e.g. "15:32"

# Output:

# d - distance

between these times in minutes.

# It is assumed that t1 is

before t2.

# Even if t1="22:00" and t2="00:45",

#

then the distance is 45 + 2*60 = 165

def mins (t1, t2):

# compte the

minutes from 00:00 till t1 and t2

m1 = int(t1[0:2])*60 + int(t1[3:5])

m2 =

int(t2[0:2])*60 + int(t2[3:5])

# if m2 > m1 then we should return

m2-m1,

# otherwise, we should return

# m2 + (number of minutes in 24

hours=60*24-1440) - m1

d = m2-m1;

if (d<0):

d=d+1440

return

d

# tests

# "22:00", "00:45" => 165

print

mins("22:00", "00:45")

# "12:45", "13:00" => 15

print

mins("12:45", "13:00")

# Input:

# t1, t2 - strings, represeting time in the form

"HH:MM",

# e.g. "15:32"

# Output:

# d - distance

between these times in minutes.

# It is assumed that t1 is

before t2.

# Even if t1="22:00" and t2="00:45",

#

then the distance is 45 + 2*60 = 165

def mins (t1, t2):

# compte the

minutes from 00:00 till t1 and t2

m1 = int(t1[0:2])*60 + int(t1[3:5])

m2 =

int(t2[0:2])*60 + int(t2[3:5])

# if m2 > m1 then we should return

m2-m1,

# otherwise, we should return

# m2 + (number of minutes in 24

hours=60*24-1440) - m1

d = m2-m1;

if (d<0):

d=d+1440

return

d

# tests

# "22:00", "00:45" => 165

mins("22:00", "00:45")

# "12:45", "13:00" => 15

mins("12:45", "13:00")

## Comments

## Leave a comment