Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Write the function readable interval() that takes a string consisting of a human-readable time interval and converts it into a list of six integers,

Python

Write the function readable interval() that takes a string consisting of a human-readable time interval and converts it into a list of six integers, given in this order: [# of years, # of months, # of days, # of hours, # of minutes, # of seconds].

The input values stored within the string can be quite scrambled and consist of multiple, repeated time components (days, months, etc.), as shown in the examples below. If a time component appears more than once, the values are be added. The time units might be given in singular or plural form (e.g., month and months are both valid). There will always be at least (but not necessarily exactly) one space between the tokens (i.e., components) of the string.

image text in transcribed

def readable_interval(interval): return None 
if __name__ == "__main__": 
 interval1 = '1 year 2 months 3 seconds'  print('Testing readable_interval() for "' + interval1 + '": ' + str(readable_interval(interval1))) interval2 = '1 second 2 years 3 seconds 7 months 6 days'  print('Testing readable_interval() for "' + interval2 + '": ' + str(readable_interval(interval2))) interval3 = ' 1 days 4 days 3 years 4 hours 17 minutes'  print('Testing readable_interval() for "' + interval3 + '": ' + str(readable_interval(interval3))) interval4 = '6 hours 20 minutes 421 seconds '  print('Testing readable_interval() for "' + interval4 + '": ' + str(readable_interval(interval4))) interval5 = '0 days 0 months 7 months 18 years'  print('Testing readable_interval() for "' + interval5 + '": ' + str(readable_interval(interval5))) 
Examples: Function Call: readable interval ('1 year 2 months 3 seconds') Return Value: 1, 2, 0, 0, 0, 3 Function Call: readable interval 1 second 2 years 3 seconds 7 months 6 days') Return Value: [2, 7, 6, 0, 0, 4 Function Call: readable interval 1 days 4 days 3 years 4 hours 17 minutes' Return Value: 3 0, 5, 4, 17 01 Function Call: readable interval 6 hours 20 minutes 421 seconds Return Value: [0, 0, 0, 6, 20 421] Function Call: days 0 months 7 months readable interval 0 18 years') Return Value: 18 7, 0, 0, 0, 0]

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