Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part II: Sleeping In (20 points) Karen loves palindromes, and she believes that it is good luck to wake up when the time is a

image text in transcribed

Part II: Sleeping In (20 points) Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome. Given the current time, the function sleep minutes ) computes and returns the number of minutes she should sleep such that, when she wakes up, the time is a palindrome. Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because 05:39 backwards is 93:50 On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50 Write a function sleep minutes ) that takes one argument: current time: a string of the form 'hh:mm', where hh is a two-digit representation of an hour of the day, and mm is a two-character representation of the minute of the hour. The argument is guaranteed to represent a valid time At some point your function will probably need to extract the hh and mm parts of current.time and convert these substrings into integers. Luckily, Python makes this string-to-integer conversion process easy. Suppose that we have a variable age, which contains the string "24". To convert this string into an integer, all we need to write is age - int (age). Now age will contain the integer 24 Now, the intention in this part of the assignment is that you call the function you implemented in Part I. Here is the basic idea of the algorithm you need to implement for Part II: convert current_time to two separate values: the number of hours (hour) and the number of minutes (minute) while hour minute_to_ str (hour, minute) is not a palindrome: advance the current time by one minute into the future update the values of hour and minute To keep track of the current time inside of the while-loop, you might find it useful to convert the initial hour and minute to a total number of minutes elapsed since midnight: hour 60 + minute. When this total number of minutes hits 24*60, we need to "wrap around" to the next day (i.e., 0 minutes, which corresponds with the time 00:00). There are a few ways we could implement this "wrapping around" idea. Give it some thought! CSE 101 - Spring 2019 Homework #3 One final hint: with string slicing notation we have a very easy way of testing if a given string is a palindrome This is simply it: if some_stringsome_ string[::-1]: # case where some-string is a palindrome else # case where some-string is not a palindrome Examples: Function Argument Return Value Explanation "05:46" 01:10" "23:52" "00:03" 20:59" The next palindromic time is a few seconds into the future (05:50) The current time is palindromic Special case: the next palindromic time is 00:00 The next palindromic time is more than a minute away (01:10). The next palindromic time is in the next minute (21:12) 67 13

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

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions