Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program can not show the output. Could anyone help me to figure out the program? The program should read input from stdin(one per line)

 
 
This program can not show the output. Could anyone help me to figure out the program? The program should read input from stdin(one per line) which may contain a date in one of the two formats below: day/month/year(e.g. 31/10/2017) day month year(e.g. 31 October 2017) files are in this link https://drive.google.com/drive/folders/0BzoFdHKM0HX5UmE2cUtMU1Y4WGc?usp=sharing The output should match sample_output.txt import re import sys month_names = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]  ''' Construct regular expressions for the following two date formats: 1. //  where  contains 4 digits;  and  contain 1 or 2 digits.  (e.g. '11/05/2017', '06/02/2000', '6/2/1999') 2.     where  contains 4 digits;  is a name for a month (see the variable  month_names);  contain 1 or 2 digits.  (e.g. '18 March 2007', '1 October 1999', '2 june 2015')  You can assume that there are no digits that precede or follow the date in the string. ''' # regular expression for pattern 1 re1 = re.compile('(\d\d|\d)[/](\d\d|\d)[/](\d\d\d\d)')  # regular expression for pattern 2 re2 = re.compile('(\d\d|\d)+\s+([a-zA-Z])+\s+(\d\d\d\d)')  # Define method def extract_date(string):  '''  Extract the date from the string using re.match(, ). Since we initialze  re1 and re2 as global variables, we can use them in this function. Then use  .group() or .groups() to get the match strings.  If there is a match, return [, , ] where ,  and   are integers.  If there is no match, return an invalid date (e.g, [0, 0, 0]).   Arg: string - a string   Return: [, , ] - a list of integers  '''   # Check for pattern 1 tp = re.search(re1, string)   # Check for pattern 2 xp = re.search(re2, string)   # Check condition if tp == None:   if xp == None:   return [0, 0, 0]  else:   stp = xp.group()  sk = stp.split(" ")  day = sk[0]  monthname = sk[1]  monthname = monthname.lower()  year = sk[2]  cnt = 0 for kk in month_names:  cnt = cnt + 1 if kk == monthname:  break  return [int(day), cnt, int(year)]   else:  return [int(tp.group(1)), int(tp.group(2)), int(tp.group(3))]  # Define method to check date def check_date(day, month, year):  '''  Check if the date is valid or not:  1. The month must be in the range [1,12].  2. Some months have 31 days, some have 30 day, one has 28 days. If you don't know the  rule, check out this link https://en.wikipedia.org/wiki/Month. You can assume that  there is no leap year in the input.   Arg: day - an integer  month - an integer  year - an integer   Return: a Boolean value  '''  # Check condition if (day == 0 or month == 0 or year == 0):  return False  if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):  if (day < 0 or day > 31):  return False  elif (month == 4 or month == 6 or month == 9 or month == 11):  if (day < 0 or day > 30):  return False  elif (year % 4 == 0 and year % 100 != 0 or year % 400 == 0):  if (day < 0 or day > 29):  return False  else:  if (day < 0 or day > 28):  return False   # return return True  def main():  '''  The function reads input from stdin (line by line), extracts date in each line,  checks if the date is valid and then output the message.  '''  input_file = sys.argv[1]  for line in input_file:  '''  #Call extract_date() to get the date information.  #Remember to remove the new line char and make  be lowercase.  '''  # Call Function [day, month, year] = extract_date(line)   '''  #If there is a valid date, output the date in the format: "(year, month, day)".  #If there is no valid date, output "no date detected".  '''   # Check condition if check_date(day, month, year):  print("(" + str(year) + "," + str(month) + "," + str(day) + ")")  else:  print("no date detected")  if __name__ == "__main__":  main() 

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

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions

Question

2. What types of information are we collecting?

Answered: 1 week ago

Question

Design a training session to maximize learning. page 296

Answered: 1 week ago

Question

Design a cross-cultural preparation program. page 300

Answered: 1 week ago