Question

PYTHON Build a regular expressions based on informal specifications to match specified patterns. Use a compiled...

PYTHON

  1. Build a regular expressions based on informal specifications to match specified patterns.
  2. Use a compiled regular expression in a Python program where appropriate.
  3. Use Python's text processing str methods to generate a string format converter.

create a date format converter. Your program will convert a date in the format “mm/dd/yyyy” to the format “month day, year”.

Specify the required input format: mm/dd/yyyy Use a regular expression to validate the user input date format. If the format is incorrect raise a SystemExit (Links to an external site.)built-in Python exception.

Split the input string into respective month, day, and year components. Using a list to hold the month format as a string, convert the month input to the correct string month name. You will need to calculate an appropriate index to retrieve the correct month name from the month list.

Use the Gregorian calendar for valid dd:

The 12 Months

The Gregorian calendar (Links to an external site.) consists of the following 12 months:

  1. January (Links to an external site.) - 31 days
  2. February (Links to an external site.) - 28 days in a common year (Links to an external site.) and 29 days in leap years (Links to an external site.)
  3. March (Links to an external site.) - 31 days
  4. April (Links to an external site.) - 30 days
  5. May (Links to an external site.) - 31 days
  6. June (Links to an external site.) - 30 days
  7. July (Links to an external site.) - 31 days
  8. August (Links to an external site.) - 31 days
  9. September (Links to an external site.) - 30 days
  10. October (Links to an external site.) - 31 days
  11. November (Links to an external site.) - 30 days
  12. December (Links to an external site.) - 31 days

Below is an example conversion:

Enter a date (mm/dd/yyyy): 01/01/2018
The converted date is: January 01, 2018

Testing Specification

Testing Requirements:  Use a loop construct to display 5 date conversions. The 1st 3 test cases should consist of valid user input. The 4th test case should be a leap year. The last 5th test case should consist of invalid user input.

Tips and Requirements

1.    A list is used to store the month string names.

2.    An index is calculated to retrieve the correct month from the list based on the user input.

3. A user prompt instructs the user of the expected input date format: mm/dd/yyyy

4. Obtain and validate user input. Specify a regular expression to validate the date format input by the user. Display an error message and raise a SystemExit sys.exit() if invalid.

5. The converted date output is in the format: month day, year

6. The output display: month is spelled out, the day is 2 digits dd and the year is 4 digits dddd.

7. Use named constants instead of literal values (i.e. NUM_DATES = 5).

8. Require 2 digits be supplied for mm and dd, 4 digits be supplied for year yyyy (require that the leftmost digit of the year not to be a 0). Use valid user input date range: 01/01/1000 - 12/31/2999.

Hand in only one .py file.

yournameLab4.py

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

CODE IN PYTHON:

import sys

def isLeaf(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return False
monthList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
numOfDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
try:
date = input("Enter a date(mm/dd/yy):")
month, day, year = tuple(date.split("/"))
if len(month) != 2 or len(day) != 2 or len(year) != 4:
sys.exit(0)
else:
newMonth = int(month)
newYear = int(year)
if isLeaf(newYear):
numOfDays[1] = 29
newMonth = monthList[newMonth - 1]
newDay = int(day)
numDays = numOfDays[monthList.index(newMonth)]
if newDay >=0 and newDay <= numDays:
print(newMonth, day, ",",year)
else:
sys.exit(0)
except Exception:
print("SystemExitException")

INDENTATION:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
PYTHON Build a regular expressions based on informal specifications to match specified patterns. Use a compiled...
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
  • JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please  ...

    JAVA CODE Beginner. please use if, else if or switch statements, but don't use arrays, please   Write a program that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Write a program that reads a string from keyboard and tests whether it contains a valid...

    Write a program that reads a string from keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be 1 from to a value that is appropriate...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates...

    I need this in Java, please! The ISO 8601 Standard date format for Information Interchange indicates that a date be written as such: yyyy-MM-dd (eg. 2012-07-02, 1999-12-05, 1998 -01-27 ) where yyyy represents the four digit year MM represents a two digit numerical month dd represents a two digit numerical day Chinese date format is specified as: yyyy-M-d Macedonean date format is specified as: d.M.yyyy where yyyy represents the four digit year M represents a one or two digit numerical...

  • Write a Java console application that reads a string from the keyboard and tests whether it...

    Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is NOT valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). You can use: String monthPart = inputDate.substring(0, 2); int month =...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

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