Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

By using Python 3.6: For this problem, you'll be defining a function that calculates the difference between two times of day, where the times of

By using Python 3.6: For this problem, you'll be defining a function that calculates the difference between two times of day, where the times of day are represented as strings in the form "h:mm", where h is a number of hours (any number of digits) and mm is the two-digit version of the minutes. So 4:08am would be represented by the string "4:08" and 9:59pm would be represented by "21:59". We want a function that when you input "4:08" and "21:59", will output "17:51"

Like any good programmer, you're not going to do everything all in one go. Instead, you'll first write a series of helper functions, And at the end, you'll put them all together in one big function. There are many ways to break down this task into smaller parts, and for future assignments, I'll let you decide exactly how to break things down. But for this assignment, I want you to break the tasks down according to these instructions, which are designed to help you learn a number of different tools. Here are the tasks we'll need helper functions for:

Given a time string, return a tuple of two integers representing the hours and minutes parts of the time.

Given an integer representing the hour and an integer representing the minutes, return a representing the time measured in minutes (e.g., given 1 and 5, return 65.)

Given two integers representing times in minutes, find the difference between them.

Given an integer representing a length of time in minutes, return a tuple of two integers representing the hours part of the time and a the minutes part of the time.

Given a tuple of integers representing hours and minutes, return a string that describes that length of time (e.g., given (17,51), return "17:51".)

a. Define a function called diff_min() that takes two integers (each representing a number of minutes) and outputs the difference in time between them.

b. Define a function time_to_str() that takes a two-item tuple (containing two integers representing a number of hours and a number of minutes) and returns a string that represents that amount of time. So for example, time_to_str((17,51)) should return "17:51" and time_to_str((3,8)) should return "3:08".

Note that I'm asking for a function that only takes one argument (a tuple containing two integers). If your function takes two arguments, it will be rejected.

Hint: There are at least two reasonable ways to handle the problem of getting a zero in front of the minutes when the number of minutes is less than 10. One strategy only involves tools we've already seen (conditionals, inequalities, and concatenation). But there is another method that involves using the .zfill() string method. If you can figure out how methods work and how to use this specific method, then feel free to use it here. However, if you have no idea what I'm talking about, you should probably try to handle this problem using the tools you're already familiar with.

c. Define a function time_to_minutes() that takes two integer inputs (a number of hours and a number of minutes) and returns the total number of minutes. So for example, time_to_minutes(2,10) should return 130.

Note that unlike in the last problem, I'm asking for a function that takes two integer arguments, not a tuple containing two integers. If your function takes a tuple of number instead of two separate numbers, it will be rejected.*

*Why are we handling things differently here than we did for time_to_str()? Because I want you to get some practice handling things in two different ways. In a real world problem, it'd probably make more sense to handle passing times the same way for both functions.

d. Define a function minutes_to_time() which returns a tuple of two integers (a number of hours and a number of minutes), given just an integer number of minutes. So minutes_to_time(327) will return (5,27) and minutes_to_time(37) will be (0,37). Note that the minutes part will always be smaller than 60.

Hint: For this one, you may find the integer division // and remainder % operators useful.

e. Define a function extract_time() that takes a string of the form "h:mm" and returns a tuple of two integers representing the integer of hours and the integer of minutes. So extract_time("15:08") should return (15,8) and extract_time("8:59") should give (8,59). You can always depend on the minutes to have two digits, but the hours portion might have any integer of digits.

Hint 1: You did most of the work for this problem in last week's lab assignment. You can reuse much of that code here. The big difference being that now you are writing only one function which returns both numbers.

Hint 2: Make sure this function is returning a tuple of integers, not a tuple of strings. If it doesn't, the rest of this problem isn't going to work properly.

f. And finally, you get to put everything together. Define a function time_between() that, given two strings in the form "h:mm" representing two times of day, computes the difference in time between them, returning its answer as a string. So for example, time_between("4:08","21:59") would return the string "17:51" and time_between("101:21","101:29") would return the string "0:08". You're allowed to assume that the second time will always be bigger than the first time.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions