Parsing dates and I am lost - python

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
This is what I have to start with and I am lost. Help?
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
user_string = input()
# TODO: Read dates from input, parse the dates to find the one
# in the correct format, and output in m/d/yyyy format

import datetime
inputs = []
result = []
#read the inputs
date = input()
inputs.append(date)
while not date == "-1":
date = input()
inputs.append(date)
#check if the input is in the correct format and convert it.
for date_text in inputs:
try:
date_text = datetime.datetime.strptime(date_text,"%d %B, %Y")
result.append(date_text.strftime("%d/%m/%Y"))
except ValueError:
pass
print(*result, sep = "\n")

s=0
d={'january':1,
'february':2,
'march':3,
'april':4,
'may':5,
'june':6,
'july':7,
'august':8,
'september':9,
'october':10,
'november':11,
'december':12}
while s!='-1':
s=input()
if "," in s:
s = s.split(",")
ar = s[0].split(" ")
if len(ar)<2:
continue
month, date = ar[0], ar[1]
year = s[1].strip()
if d.get(month.lower()):
print("{}/{}/{}".format(date, d[month.lower()], year))

Related

ZyBooks Lab:Seasons assignment - What's wrong with my code

Lab:Seasons assignment
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day.
Ex: If the input is:
April
11
the output is:
Spring
In addition, check if the string and int are valid (an actual month and day).
Ex: If the input is:
Blue
65
the output is:
Invalid
The dates for each season are:
Spring: March 20 - June 20
Summer: June 21 - September 21
Autumn: September 22 - December 20
Winter: December 21 - March 19
my code so far:
input_month = input()
input_day = int(input())
months = ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December')
if not (input_month in months):
print ('Invalid')
elif input_month == 'January':
if not (0 < input_day <= 31):
print('Invalid')
else:
print('Winter')
elif input_month == 'February':
if not (0 < input_day <= 29):
print('Invalid')
else:
print('Winter')
elif input_month == 'March':
if (0 < input_day < 20):
print('Winter')
elif (20 <= input_day <= 31):
print('Spring')
else:
print('Invalid')
elif input_month == 'April':
if not (0 < input_day <= 30):
print('Invalid')
else:
print('Spring')
elif input_month == 'May':
if not (0 < input_day <= 31):
print('Invalid')
else:
print('Spring')
elif input_month == 'June':
if (0 < input_day <= 20):
print('Spring')
elif (20 < input_day <= 30):
print('Summer')
else:
print('Invalid')
elif input_month == 'July' or 'August':
if not (0 < input_day <= 31):
print('Invalid')
else:
print('Summer')
elif input_month == 'September':
if not(0 < input_day <= 30):
print("Invalid")
elif (0 < input_day <= 21):
print ("Summer")
else:
print ("Autumn")
elif input_month == 'October':
if not (0 < input_day <= 31):
print('Invalid')
else:
print('Autumn')
elif input_month == 'November':
if not (0 < input_day <= 30):
print('Invalid')
else:
print('Autumn')
elif input_month == 'December':
if (0 < input_day <= 20):
print('Autumn')
elif (20 < input_day <= 31):
print('Winter')
else:
print('Invalid')
I am getting errors for September 31 and November 7th and I can't figure out why?

Is there a way to break from a range() function at a specific value?

I am new to programming in python and am trying to design a calendar that starts the month depending on the selected start day.
However, I don't know how to stop the print once the number of days has been exceeded(e.g breaks at days=31 when month=="January")
The printed values must be right-aligned additionally.
Here is how I first approached it:
month=input("Enter the month: ")
if month=="January" or month=="March" or month=="May" or month=="July" or month=="August" or month=="October" or month=="December":
days=31
else:
days=30
if month=="February":
days=28
Start_day=input("Enter the start day: ")
print(month)
print("Mo","Tu","We","Th","Fr","Sa","Su")
if Start_day == "Monday":
i=1
if Start_day == "Tuesday":
i=0
if Start_day == "Wednesday":
i=-1
if Start_day == "Thursday":
i=-2
if Start_day == "Friday" :
i=-3
if Start_day == "Saturday":
i=-4
if Start_day == "Sunday":
i=-5
j=1
for j in range(i,days,7):
print(str(j).rjust(2," "),str(j+1).rjust(2," "),str(j+2).rjust(2," "),str(j+3).rjust(2," "),str(j+4).rjust(2," "),str(j+5).rjust(2," "),str(j+6).rjust(2," "))
You could encode it instead as
j=1
for j in range(i,days,7):
for i in range(0,7):
if j+i>days: break
print(str(j+i).rjust(2," "),end=' ')
print('')
This would be called "breaking out of a loop" rather than "breaking from a range function." There is no way to "break from a range function."
Can I suggest overhauling this a bit to be more efficient? You can use dicts and define a custom function to handle the date formatting to prevent some repetition.
To answer your question, you can evaluate the date number during the final loop:
for j in range(i,days,7):
# add to j value via range() and adjust()
# (defined above) to prevent repetition
for k in range(7):
if j + k > 0 and j + k <= days:
print(adjust(j + k), end = ' ') # don't print new line
else:
# print spaces if the number is <1 or >days
print(' ', end = '')
# print new line for a new week
print('\n', end = '')
Full example:
# function to format dates later
def adjust(val):
return str(val).rjust(2," ")
# get inputs
month=input("Enter the month: ")
start_day=input("Enter the start day: ")
# map months to days in a dict
month_to_days={"january":31,
"march":31,
"may":31,
"july":31,
"august":31,
"october":31,
"december":31,
"february":28,
"april":30,
"june":30,
"september":30,
"october":30
}
# map weekdays to int
days_to_int={"monday":1,
"tuesday":0,
"wednesday":-1,
"thursday":-2,
"friday":-3,
"saturday":-4,
"sunday":-5
}
# get the day amount based on the entry, ignoring case
days=month_to_days[month.lower()]
# get the int based on the entry, ignoring case
i=days_to_int[start_day.lower()]
# print month and day headers
print(month)
print("Mo","Tu","We","Th","Fr","Sa","Su")
for j in range(i,days,7):
# add to j value via range() and adjust()
# (defined above) to prevent repetition
for k in range(7):
if j + k > 0 and j + k <= days:
print(adjust(j + k), end = ' ') # don't print new line
else:
# print spaces if the number is <1 or >days
print(' ', end = '')
# print new line for a new week
print('\n', end = '')
Output:
Enter the month: january
Enter the start day: monday
january
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
>>>
Enter the month: june
Enter the start day: wednesday
june
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Before you print after your for loop just have an if statement to check for your condition and before you print add a break statement.
Something like:
if statement:
break

Udacity's finish daysBetweenDates answer might be wrong

There might be a bug in this lesson.
I'm trying to move forward without needing to watch the answer videos for the daysBetweenDates quiz.
Long story short:
I figured out the code and it works for all test cases, except one.
The error was odd. The difference between the number of days between dates I got and the answer according to Udacity. My notion is that if there's error in my code, the difference between the answer I got and Udacity's expected answer would be more than 1 because the error should be recurring.
I tried to compute the number of days between dates using a different approach. I got the number my program computed.
So the question is, is the number of days between dates between 1900,1 ,1 and 1999,12, 31 really 36523(Udacity's answer) or 36524(my answer)
Here's my complete code
I recommend you try it on your interpreter to check if Udacity's answer for the last test case is correct.
# Credit goes to Websten from forums
#
# Use Dave's suggestions to finish your daysBetweenDates
# procedure. It will need to take into account leap years
# in addition to the correct number of days in each month.
number_of_days_in_month = 30
def nextDay(year, month, day):
"""Simple version: assume every month has 30 days"""
number_of_days_in_month = setDaysInMonth(month, year)
if day < number_of_days_in_month:
return year, month, day + 1
else:
if month == 12:
return year + 1, 1, 1
else:
return year, month + 1, 1
def dateIsBefore(year1, month1, day1, year2, month2, day2):
"""Returns True if year1-month1-day1 is before year2-month2-day2. Otherwise, returns False."""
if year1 < year2:
return True
if year1 == year2:
if month1 < month2:
return True
if month1 == month2:
return day1 < day2
return False
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
"""Returns the number of days between year1/month1/day1
and year2/month2/day2. Assumes inputs are valid dates
in Gregorian calendar."""
# program defensively! Add an assertion if the input is not valid!
assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
number_of_days_between_dates = 0
while dateIsBefore(year1, month1, day1, year2, month2, day2):
year1, month1, day1 = nextDay(year1, month1, day1)
number_of_days_between_dates += 1
print number_of_days_between_dates
return number_of_days_between_dates
def setDaysInMonth(month1, year1):
if isLeapYear(year1) == False:
if month1 == 1:
number_of_days_in_month = 31
if month1 == 3:
number_of_days_in_month = 31
if month1 == 5:
number_of_days_in_month = 31
if month1 == 7:
number_of_days_in_month = 31
if month1 == 8:
number_of_days_in_month = 31
if month1 == 10:
number_of_days_in_month = 31
if month1 == 12:
number_of_days_in_month = 31
if month1 == 4:
number_of_days_in_month = 30
if month1 == 6:
number_of_days_in_month = 30
if month1 == 9:
number_of_days_in_month = 30
if month1 == 11:
number_of_days_in_month = 30
if month1 == 2:
number_of_days_in_month = 28
return number_of_days_in_month
else:
if month1 == 1:
number_of_days_in_month = 31
if month1 == 3:
number_of_days_in_month = 31
if month1 == 5:
number_of_days_in_month = 31
if month1 == 7:
number_of_days_in_month = 31
if month1 == 8:
number_of_days_in_month = 31
if month1 == 10:
number_of_days_in_month = 31
if month1 == 12:
number_of_days_in_month = 31
if month1 == 4:
number_of_days_in_month = 30
if month1 == 6:
number_of_days_in_month = 30
if month1 == 9:
number_of_days_in_month = 30
if month1 == 11:
number_of_days_in_month = 30
if month1 == 2:
number_of_days_in_month = 29
return number_of_days_in_month
def isLeapYear(year1):
if year1 % 4 == 0:
return True
return False
def numberOfLeapYears(year1, year2):
number_of_leap_years = 0
while year1 < year2:
if year1 % 4 == 0:
number_of_leap_years += 1
year1 += 1
else:
year1 += 1
#print "number of leap years: " + str(number_of_leap_years)
return number_of_leap_years
def numberOfNonLeapYears(year1, year2):
number_of_non_leap_years = 0
while year1 < year2:
if year1 % 4 == 0:
year1 += 1
else:
number_of_non_leap_years += 1
year1 += 1
#print "number of non leap years: " + str(number_of_non_leap_years)
return number_of_non_leap_years
def numberOfDays(year1, year2):
number_of_leap_years = numberOfLeapYears(year1, year2)
print number_of_leap_years
number_of_non_leap_years = numberOfNonLeapYears(year1, year2)
print number_of_non_leap_years
number_of_days = 0
number_of_days = number_of_leap_years * 366 + number_of_non_leap_years * 365
#print number_of_days
return number_of_days
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523),
((1900,1,1,1910,1,1), 3653)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
#print isLeapYear(1900)
#print 366*3 + 365*7
#print numberOfLeapYears(1900, 1999)
#print numberOfNonLeapYears(1900, 1999)
print numberOfDays(1900, 2000)
You are using a naive test for Leap Year ( %4). But there are additional rules to leap years.
Leap year is any year evenly divisible by 4, unless it is divisible by 100 (then it is not a leap year), unless it is divisible by 400 (Then it is a leap year).
Thus 1500, 1700, 1800, 1900 were not leaps years but 1600 and 2000 were leap years.
A simple Python version:
def is_leap_year(year):
if year % 400 == 0:
return True
if year % 4 == 0 and not year % 100 == 0:
return True
return False

How to assign months to their numeric equivalents in Python / Pandas?

Currently, I'm using the following for loop based on an if condition for each month to assign months to their numeric equivalents. It seems to be quite efficient in terms of runtime, but is too manual and ugly for my preferences.
How could this be better executed? I imagine it's possible to improve on it by simplifying/condensing the multiple if conditions somehow, as well as by using some sort of translator that is made for date conversions? Each of which would be preferable?
#make numeric month
combined = combined.sort_values('month')
combined.index = range(len(combined))
combined['month_numeric'] = None
for i in combined['month'].unique():
first = combined['month'].searchsorted(i, side='left')
last = combined['month'].searchsorted(i, side='right')
first_num = list(first)[0] #gives first instance
last_num = list(last)[0] #gives last instance
if i == 'January':
combined['month_numeric'][first_num:last_num] = "01"
elif i == 'February':
combined['month_numeric'][first_num:last_num] = "02"
elif i == 'March':
combined['month_numeric'][first_num:last_num] = "03"
elif i == 'April':
combined['month_numeric'][first_num:last_num] = "04"
elif i == 'May':
combined['month_numeric'][first_num:last_num] = "05"
elif i == 'June':
combined['month_numeric'][first_num:last_num] = "06"
elif i == 'July':
combined['month_numeric'][first_num:last_num] = "07"
elif i == 'August':
combined['month_numeric'][first_num:last_num] = "08"
elif i == 'September':
combined['month_numeric'][first_num:last_num] = "09"
elif i == 'October':
combined['month_numeric'][first_num:last_num] = "10"
elif i == 'November':
combined['month_numeric'][first_num:last_num] = "11"
elif i == 'December':
combined['month_numeric'][first_num:last_num] = "12"
You can use to_datetime, then month, convert to string and use zfill:
print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2))
Sample:
import pandas as pd
df = pd.DataFrame({ 'month': ['January','February', 'December']})
print (df)
month
0 January
1 February
2 December
print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2))
0 01
1 02
2 12
Name: month, dtype: object
Another solution is map by dict d:
d = {'January':'01','February':'02','December':'12'}
print (df['month'].map(d))
0 01
1 02
2 12
Name: month, dtype: object
Timings:
df = pd.DataFrame({ 'month': ['January','February', 'December']})
print (df)
df = pd.concat([df]*1000).reset_index(drop=True)
print (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2))
print (df['month'].map({'January':'01','February':'02','December':'12'}))
In [200]: %timeit (pd.to_datetime(df['month'], format='%B').dt.month.astype(str).str.zfill(2))
100 loops, best of 3: 13.5 ms per loop
In [201]: %timeit (df['month'].map({'January':'01','February':'02','December':'12'}))
1000 loops, best of 3: 462 µs per loop
You can use a map:
month2int = {"January":1, "February":2, ...}
combined["month_numeric"] = combined["month"].map(month2int)

Days old udacity

I have a problem in these two casesprint daysBetweenDates(2011, 1, 1, 2012, 8, 8)
print daysBetweenDates(1900,1,1, 1999,12, 31)when I put them with the other test cases I got a wrong answer by 1 day extra and sometimes by 2 days.sometimes one of them give me the right answer but it also appears asTest with data:(2011, 1, 1, 2012,8,8)failed
Test with data: (1900, 1, 1, 1999, 12, 31) failed but when I test each case alone i got the right answer.
daysofmonths = [ 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def leap_year(year):
leap_day = 366
common_day = 365
if year % 4 != 0:
return common_day
elif year % 100 != 0:
return leap_day
elif year % 400 !=0:
return common_day
else:
return leap_day
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
#code for same year
if year1 == year2:
if month1 == month2:
return day2 - day1
days = daysofmonths[month1] - day1
month1 = month1 + 1
while month1 < month2:
if leap_year(year1) == 366:
daysofmonths[2] = 29
days = days + daysofmonths[month1]
month1 = month1 + 1
return days + day2
################################################
days = daysofmonths[month1] - day1
month1 = month1 + 1
while month1 <= 12:
if leap_year(year1) == 366:
daysofmonths[2] = 29
days = days + daysofmonths[month1]
month1 = month1 + 1
#print days
year1 = year1 + 1
###########################################################
days = days + day2
month2 = month2 - 1
while month2 >= 1:
if leap_year(year2) == 366:
daysofmonths[2] = 29
days = days + daysofmonths[month2]
month2 = month2 - 1
#print days
year2 = year2 - 1
###########################################################
while year1 <= year2:
days = days + leap_year(year1)
year1 = year1 + 1
return days
print daysBetweenDates(2011, 1, 1, 2012, 8, 8)
print daysBetweenDates(1900,1,1, 1999,12, 31)
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
when you do:
daysofmonths[2] = 29
it changes the element in the list, which then is used for every subsequent call, if you added print(daysofmonths[2]) in between the test cases you would see that it is always 29 after the first case that needs to check February, so instead of conditionally changing the list with:
if leap_year(year1) == 366:
daysofmonths[2] = 29
days = days + daysofmonths[month1]
just conditionally add to days:
if leap_year(year1) == 366 and month1 == 2:
days = days + 29
else:
days = days + daysofmonths[month1]
then do the same thing lower down with year2 and month2 (I would highly recommend you separate your code into more functions as a lot of it is very repetitive)
This is the only post I found here on this particular problem so I thought I would share my solution.
#days in the months of a non leap year
daysOfMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#determine if a year is a leap year
def is_leap_year(year1):
year = True
if year1 % 4 != 0:
year = False
elif year1 % 100 != 0:
year = True
elif year1 % 400 != 0:
year = False
else: year = True
return year
#returns the days in the given month of the given year
#I was trying to do something similar to the OP until I read this post
def days_in_month(year, month):
days = 0
if is_leap_year(year) and month == 2:
days += 29
else:
days += daysOfMonths[month - 1]
return days
#itterates through each month starting at year1 month1
#up to but not including month2 of year2 and
#returns the total number of days in that period
def total_days(year1, month1, year2, month2):
days = 0
while year1 < year2 or month1 < month2:
days += days_in_month(year1, month1)
month1 += 1
if month1 == 13:
year1 += 1
month1 = 1
return days
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
days = total_days(year1, month1, year2, month2)
#because I included the beginning month I have to subtract day1
#because I did not include the final month I have to add day2
return days - day1 + day2
#I used print statements here to troubleshoot
#print days_in_month(2012, 1)
#print daysBetweenDates(2012, 1, 1, 2012, 2, 28)
#print daysBetweenDates(2012, 1, 1, 2012, 3, 1)
#print daysBetweenDates(2011,6,30,2012,6,30)
#print daysBetweenDates(2011,1,1,2012,8,8)
#print daysBetweenDates(1900,1,1,1999,12,31)
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2012,1,1,2012,3,1), 60),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()

Categories