'Date Parsing Practice

Problem:

Write a program to read dates from input, one date per line. Each date's format must be as follows: March 1, 1990. Any date not following that format is incorrect and should be ignored. The input ends with -1 on a line alone. Output each correct date as: 3/1/1990.

Hint: Use string[start:end] to get a substring when parsing the string and extracting the date. Use the split() method to break the input into tokens.

Ex: If the input is:

March 1, 1990
April 2 1995
7/15/20
December 13, 2003
-1

then the output is:

3/1/1990
12/13/2003

My code so far:

def get_month_as_int(monthString):

    if monthString == 'January':
        month_int = 1
    elif monthString == 'February':
        month_int = 2
    elif monthString == 'March':
        month_int = 3
    elif monthString == 'April':
        month_int = 4
    elif monthString == 'May':
        month_int = 5
    elif monthString == 'June':
        month_int = 6
    elif monthString == 'July':
        month_int = 7
    elif monthString == 'August':
        month_int = 8
    elif monthString == 'September':
        month_int = 9
    elif monthString == 'October':
        month_int = 10
    elif monthString == 'November':
        month_int = 11
    elif monthString == 'December':
        month_int = 12
    else:
        month_int = 0

    return month_int


monthString = input()

# TODO: Read dates from input, parse the dates to find the one
#       in the correct format, and output in m/d/yyyy format
user_string = monthString.split()

monthString = user_string[0]

while monthString in get_month_as_int(monthString):
    if monthString == get_month_as_int(monthString):
        if user_string[1][-1] == ",":
            print("{}/{}/{}".format(get_month_as_int(monthString), user_string[1][-2:], user_string[2]))
    user_string = user_string.split()
    if user_string[0] == -1:
        break
    else:
        break

Right now I'm getting a TypeError stating argument of type 'int' is not iterable.

And I can't use the datetime function in this case.



Solution 1:[1]

months = {
    "January": 1,
    "February": 2,
    "March": 3,
    "April": 4,
    "May": 5,
    "June": 6,
    "July": 7,
    "August": 8,
    "September": 9,
    "October": 10,
    "November": 11,
    "December": 12,
}

import re
for data in ['March 1, 1990', 'April 2 1995', '7/15/20', 'December 13, 2003', -1]:
    if data == -1:
        break
    if re.match(r'(\w+) (\d{1,2}), (\d{4})', data) == None:
        continue
    output = data.split(" ")
    output[0] = str(months[output[0]])
    output[1] = output[1].replace(",", "")
    print("/".join(output))

Solution 2:[2]

def get_month_as_int(monthString):

    if monthString == 'January':
        month_int = 1
    elif monthString == 'February':
        month_int = 2
    elif monthString == 'March':
        month_int = 3
    elif monthString == 'April':
        month_int = 4
    elif monthString == 'May':
        month_int = 5
    elif monthString == 'June':
        month_int = 6
    elif monthString == 'July':
        month_int = 7
    elif monthString == 'August':
        month_int = 8
    elif monthString == 'September':
        month_int = 9
    elif monthString == 'October':
        month_int = 10
    elif monthString == 'November':
        month_int = 11
    elif monthString == 'December':
        month_int = 12
    else:
        month_int = 0

    return month_int


for monthString in iter(input):
    if(monthString == "-1"):
        break

    month = get_month_as_int(monthString.split()[0])
    print("{}/{}/{}".format(month,
                            monthString.split()[1].replace(',', ''), monthString.split()[2]))
    pass

Your original code was very close but your input logic was slightly flawed and it was not not allowing the input to continue. Here I have addressed that issue by using the iter() function https://www.w3schools.com/python/ref_func_iter.asp to handle the input. Another issue was handling the comma in the input which I cleaned up using the .replace() function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MoRe
Solution 2