Question: Python Add assert statements to the start of 'time_to_minutes' enforcing the preconditions. def valid_format(s): Returns True if s is a string in 12-format :

Python

Add assert statements to the start of 'time_to_minutes' enforcing the preconditions.

def valid_format(s): """ Returns True if s is a string in 12-format : AM/PM

Example: valid_format('2:45 PM') returns True valid_format('2:45PM') returns False valid_format('14:45') returns False valid_format('14:45 AM') returns False valid_format(245) returns False

Parameter s: the candidate time to format Precondition: NONE (s can be any value) """ if type(s) != str: return False colon = introcs.find_str(s,':') if colon == -1: return False elif not introcs.isdigit(s[:colon]) or not introcs.isdigit(s[colon+1:colon+3]): return False hrs = int(s[:colon]) mins = int(s[colon+1:colon+3]) if hrs < 1 or hrs > 12 or mins >= 60: return False return s[colon+3:] == ' AM' or s[colon+3:] == ' PM'

def time_to_minutes(s): """ Returns the number of minutes since midnight Examples: time_to_minutes('2:45 PM') returns 885 time_to_minutes('9:05 AM') returns 545 time_to_minutes('12:00 AM') returns 0 Parameter s: string representation of the time Precondition: s is a string in 12-format ': AM/PM' """

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!