Question

PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a...

PYTHON 3!!!!! Write a function:

class Solution { public String solution(String T); }

that, given a string T, returns the latest valid time that can be obtained from T, as a string in the format "HH:MM", where HH denotes a two-digit value for hours and MM denotes a two-digit value for minutes.

Examples:

1. Given T = "2?:?8", the function should return "23:58".

2. Given T = "?8:4?", the function should return "18:49".

3. Given T = "??:??", the function should return "23:59".

4. Given T = "06:34", the function should return "06:34".

Assume that:

T consists of exactly five characters; the third one is ':'; the others are digits (0-9) or '?';
there always exists a valid time obtained by substituting '?' with digits.

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem in Python 3 (AS REQUESTED). Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

''' required code in Python '''
class Solution:
    #required method
    def solution(self, T:str) -> str:
        #creating an empty string
        result=''
        #checking if first character is '?'
        if T[0]=='?':
            #appending '2' to result if second character is also '?'
            if T[1]=='?':
                result+='2'
            else:
                #otherwise extracting second digit as int
                digit=int(T[1])
                #if digit is less than or equal to 3, appending '2', else appending '1'
                if digit<=3:
                    result+='2'
                else:
                    result+='1'
        #otherwise (T[0] is not '?') appending T[0] to result
        else:
            result+=T[0]
        #checking if second char is '?'
        if T[1]=='?':
            #if first char in result is '2', appending 3 (max hour value: 23)
            if result[0]=='2':
                result+='3'
            #otherwise appending '9' (for hours 10-19)
            else:
                result+='9'
        #otherwise appending T[1]
        else:
            result+=T[1]

        #appending a colon
        result+=':'

        #if char at index 3 is ?,appending '5' (for minutes 50-59)
        if T[3]=='?':
            result+='5'
        #otherwise appending T[3] as it is
        else:
            result+=T[3]

        #if char at index 4 is '?', appending '9', else T[4]
        if T[4]=='?':
            result+='9'
        else:
            result+=T[4]

        #returning the result
        return result

Add a comment
Know the answer?
Add Answer to:
PYTHON 3!!!!! Write a function: class Solution { public String solution(String T); } that, given a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution...

    Hi, I have Python programming problem as follow: Thank you. Best Regards. Write a function solution that, given two integers A and B, returns a string containing exactly A letters 'a' and exactly B letters 'b' with no three consecutive letters being the same in other words, neither "aaa" nor "bbb" may occur in the returned string). Examples: 1. Given A = 5 and B = 3, your function may return "aabaabab". Note that "abaabbaa" would also be a correct...

  • Python Program 5. Write a Python program in a file named validTime.py. Include a function named...

    Python Program 5. Write a Python program in a file named validTime.py. Include a function named string parameter of the form hh:mm: ss in which these are numeric validTime that takes a values and returns True or False indicating whether or not the time is valid. The number of hours, minutes, and seconds must two digits and use a between 0 and 9). The number of hours must be between 0 and 23, the number of minutes and seconds must...

  • Please solve in Python. You would like to set a password for an email account. However,...

    Please solve in Python. You would like to set a password for an email account. However, there are two restrictions on the format of the password. It has to contain at least one uppercase character and it cannot contain any digits. You are given a string S consisting of N alphanumerical characters. You would like to find the longest substring of Sthat is a valid password. A substring is defined as a contiguous segment of a string. For example, given...

  • In Python 3, write a function that will take an integer which represents a time given...

    In Python 3, write a function that will take an integer which represents a time given in military time (0 – 2359) and return a string that represents the time in a 12-hour format. If an invalid time is given, the function must return the string “Invalid time”. Examples 1200 will return the string “12:00 PM” 2240 will return the string “10:40 PM” 0600 will return the string “06:00 AM” 2400 will return the string “Invalid time” 1160 will return...

  • In python Write a function called removeExtensión that is given the name of a file as...

    In python Write a function called removeExtensión that is given the name of a file as a string and returns the same string without the extension (the last "" and any following characters); if the given string has no extension, return the given string without changing it. For example, removeExtension"Chapter 3.9.txt") should return "Chapter 3.9" while removeExtension("Hello") should return "Hello"

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • edef extract_num(s, begin, end): Given string s, and "begin" is the index of the first of...

    edef extract_num(s, begin, end): Given string s, and "begin" is the index of the first of one or more digits, and "end" is the index one beyond the last digit. Parse out and return the int value of the number, accounting for possible 's' and 'a', Return -1 if the number should be skipped. >>> extract_nun('xx123$, 2, 5) 321 >>> # add Doctests here 29 def. parse line(s): Given a string s, parse the ints out of it and return...

  • please use python Question 4: Define a function named q40 that accepts a string as a...

    please use python Question 4: Define a function named q40 that accepts a string as a parameter. After verifying that the string includes only includes numeric digits, count the number of even (E) and odd (O) digits. Finally, create a new string made up of these values and their sum. Repeat this process until your end result is a string containing 123 For example, '15327' contains 1 even digit (E-1), 4 odd digits(O-4) and the sum of these is 5...

  • Create a function called countWords(string): That will count words in given string by user. Use Python...

    Create a function called countWords(string): That will count words in given string by user. Use Python 3.6 If user inputs "Mary had a little lamb" it should return 5

  • Python 3: Write a function called every_other_character() that takes in a string and returns a new...

    Python 3: Write a function called every_other_character() that takes in a string and returns a new string with every other character removed. That is, the new string will include the first, third, fifth, etc. letters of the given string. Must use a while or for loop in the solution.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT