top of page

Time evaluation

Prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console.

Key:

  • (hour + dura)//60 will give you the number of hours,

  • You'll also have to check the hours for %24 to make sure you roll the hours over.

  • (mins + dura)%60 will give you the number of minutes.

  • You'll also have to check the minutes for %60 to make sure you roll minutes over.

 

Example:

hour = int(input("Starting time (hours): ")) mins = int(input("Starting time (minutes): ")) dura = int(input("Event duration (minutes): "))

# find a total of all minutes

mins = mins + dura%60

# find a number of hours hidden in minutes and update the hour

hour = hour + dura//60 + mins//60

# correct minutes to fall in the (0..59) range mins = mins%60

# correct hours to fall in the (0..23) range hour = hour%24 print(hour, ":", mins, sep='')

 

This is the standard way to do these kinds of problems.

Division tells you how many times one number can fit inside another, like 20 // 3 = 6, so 3 can fit inside 20, 6 times.

Then modulo gives you the remainder of that operation. It's like if you kept doing 20 - 3, 17 - 3, 14 - 3, 11 - 3, 8 - 3, 5 - 3, then you get 2 and you're done, you can't take any more 3s away without going negative, so the remainder is 2. That's what the modulo gives you. 20%3 = 2.

So division and modulo are kinda 2 sides of the same coin here, the division handles the "big" part and modulo handles the "small" leftover.

Adding all the minutes and doing division will tell you have how many times around the clock you looped (hours), and then the modulo is the remainder, the minutes you're left at.

 

Here's an example: 17:52, duration: 8 hours 19 minutes

(499 minutes => 499%60 = 19 minutes)

  • Minutes: 52+19 = 71 => 71 % 60 = 11

  • Hours: 71 // 60 = 1 => 17 + (8 + 1) = 26 hours, 11 minutes.

You'll want to then do 26 % 24 for the final time 02:11.

 

 

aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page