- Write a function validate_date(date_string) which takes in a datestring and checks if the datestring is valid. If the string is valid, we want to return a tuple containing True and the date_string itself. If the string is invalid for a specific reason, we want to return a tuple containing False and the error code explaining what was wrong with the date string passed in.
Below is the error codes for reasons why a string can be invalid (based on criteria on validity, read above):
-1 --> date_string does not separate the day, month, and year with slashes. -2 --> day, month, or year are not numeric. -3 --> month out of range (not between 1 and 12) -4 --> day is out of range (not between 1 and last day of the month)
For instance, if validate_date(date_string) is called with "01/12/2022" as the argument, the return value is
(True, "01/12/2022")
because "01/12/2022" is a valid string.
If validate_date(date_string) is called with "01/44/2022" as the argument, the return value is
(False, -4)
because "01/44/2022" is an invalid string because the day (44) was out of range. So the error code is -4.
If validate_date(date_string) is called with "January/12/2022" as the argument, the return value is
(False, -2)
because "January/12/2022" is an invalid string because the month is not numeric. So the error code is -2.
Instructions
- Finish the validate_date() function
- Use the dictionary num_days that maps months to the number of days in that month (useful for checking if the day is in the range)
- You can check if the day, month, and year are separated by slashes by calling .split( "/" ) on the datestring. If the list that is returned by the .split() call is of size 3, that means there are two slashes somewhere in the string that separate the day, month, and year. If the list is not of size 3, you can be sure that datetime is invalid (Error code: -1)
def validate_date(date_string):
num_days = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31
}
month = len(date_string[0:2])
days = len(date_string[3:5])
if not date_string[0].isdigit() or not date_string[1].isdigit():
return (False, -2)
if not date_string[3].isdigit() or not date_string[4].isdigit():
return (False, -2)
if not date_string[6].isdigit() or not date_string[7].isdigit() or not date_string[8].isdigit() or not date_string[9].isdigit():
return (False, -2)
if month not in num_days:
return (False, -3)
if (days < 1) or days > num_days[month]:
return (False, -4)
else:
return (True, date_string)
# finish the function
if __name__ == "__main__":
assert validate_date('01/12/2022')[0] == True
assert validate_date('01/12/2022')[1] == '01/12/2022'
assert validate_date('01/44/2022')[0] == False
assert validate_date('01/44/2022')[1] == -4
assert validate_date('January/12/2022')[0] == False
assert validate_date('January/12/2022')[1] == -2
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- please create the c++ code regarding this screenshot question i added and also please add professional comments at each line of the code using // commandarrow_forwardWrite the convert_word code. convert_word is known as a function, which we will cover more in depth later. You are automatically given a parameter called word, which is a string that contains a single word. The return conv is how you will send data out of your function. The conv is a variable that hasn't been created yet, but it will be a string containing the converted hey. For example, if word="hey", then conv will be "heey". You are responsible for creating the variable conv, as the template has NOT done that for you. You need to use an if statement to see if the word starts with "he" and ends with "y". Since you don't know how many e's might be between the he and y, you have to do some math here. Recall that you can get the length of a string by using len(word) in the code above. You also know that h and y will be included. so, len(word) - 2 will be the number of e's in between the h and y. Create a string that contains the number of e's. This will be your conv variable. I would…arrow_forward1. What does the following code segment return assuming the argment passed is a single word or short phrase def mystery3(word: str) -> str:output = ""for char in word:output = char + outputreturn output 2. Write a function called removeVowels that accepts a string as a parameter and return a copy of that string with no vowels (aeiou) in it. Upper case and lower case vowels will be remove and the rest of the string is not changed. For example removeVowels("The Quick Brown Fox) would return "Th Qck Brwn Fx"arrow_forward
- Implement the Function has_error Implement the function according to the specification. Use the test script testcurrency.py to aid your development before checking your answer below. CANNOT USE IF/CONDITIONAL STATEMENTS Instead, you should use what you know about boolean expressions def has_error(json):"""Returns True if the response to a currency query encountered an error. Given a JSON string provided by the web service, this function returns True if thequery failed and there is an error message. For example, if the json is '{"success":false,"src":"","dst":"","error":"Source currency code is invalid."}' then this function returns True (It does NOT return the error message'Source currency code is invalid'). On the other hand if the json is '{"success": true, "src": "2 United States Dollars", "dst": "1.772814 Euros", "error": ""}' then this function returns False. The web server does NOT specify the number of spaces after the colons. The JSON '{"success":true, "src":"2 United…arrow_forwardCodingarrow_forwardCreate a function that determines how many number pairs are embedded in a space-separated string. The first numeric value in the space-separated string represents the count of the numbers, thus, excluded in the pairings. Examples number_pairs("7 1 2 1 2 1 3 2") 2 // (1, 1), (2, 2) number_pairs ("9 10 20 20 10 10 30 50 10 20") 3 // (10, 10), (20, 20), (10, 10) number_pairs("4 2 3 4 1") → 0 // Although two 4's are present, the first one is discounted.arrow_forward
- Declare a function named contains_char. Its purpose is when given two strings, the first of any length, the second a single character, it will return True if the single character of the second string is found at any index of the first string, and return False otherwise.arrow_forwardC++arrow_forwardIn C language / Please don't use (sprint) function. Write a function fact_calc that takes a string output argument and an integer input argument n and returns a string showing the calculation of n!. For example, if the value supplied for n were 6, the string returned would be 6! 5 6 3 5 3 4 3 3 3 2 3 1 5 720 Write a program that repeatedly prompts the user for an integer between 0 and 9, calls fact_calc and outputs the resulting string. If the user inputs an invalid value, the program should display an error message and re-prompt for valid input. Input of the sentinel -1 should cause the input loop to exit. Note: Don't print factorial of -1, or any number that is not between 0 and 9. SAMPLE RUN #4: ./Fact Interactive Session Hide Invisibles Highlight: None Show Highlighted Only Enter·an·integer·between·0·and·9·or·-1·to·quit:5↵ 5!·=·5·x·4·x·3·x·2·x·1·x··=·120↵ Enter·an·integer·between·0·and·9·or·-1·to·quit:6↵ 6!·=·6·x·5·x·4·x·3·x·2·x·1·x··=·720↵…arrow_forward
- In this assignment you will be responsible for writing several string validation and manipulationfunctions. The assumption will be that you are writing functions that will take input from a form andmanipulate or validate the information entered before it is processed into a database. A database is acollection of information but the information in the database must always be entered in a specificformat. Your responsibility will be to take data entered into a program and be sure it is writtencorrectly before it is entered into the database.Each function will have to have a specific name and heading. Each function will also need to includea docstring, the correct logic, and the proper return. Each function will be given a specific set ofpreconditions and postconditions/returns. A precondition is something you may assume to be truewhen the function is executed. A post condition is something you need to be sure is completed whenthe function is executed. Be sure to test each one of your…arrow_forwardWrite the function data_tuple_from_line(line_str) that takes a line of input (with value separated by commas) and returns a tuple containing install_id, install_date, size, cost, subsidy, zip code, city, state. In the result tuple: The install_id, install_date, zip code, city, and state should all be strings. The size, cost, and subsidy should all be floats. The city should be in title case.arrow_forwardConsider the following line of code: X = new int*[n]; What should the type of x be? O int O int* O int** O int& O None of the abovearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education