Reputation: 783
I was working on the following problem in Python:
Paying the rent
You are moving to a new city for an internship for a few months and have to rent a house for that purpose.
You have to pay the full month's house rent even if you have lived for a few days for that month. i.e. if you start on 15th Jan and leave by 15th May, you still have to pay full rent for months of Jan and May too.
Your task is to find the months that you have to pay rent for so that you can write post-dated cheques to your landlord.
You will be given two dates as input and your task is to print all months between the dates including the months the dates are from.
The input will contain the two dates in a list as follows: [2017,1,1,2017,3,4] which corresponds to 1st Jan, 2017 and 4th March, 2017. This date is in the format(yyyy,mm,dd)
The output should contain a list with names of months you have to pay the rent for (the list should be sorted chronologically based on months, i.e May should come before August).
You can assume that there won't be more than 12 months between two dates.
What I did:
import datetime, time
import ast,sys
input_str = sys.stdin.read()
input_list = ast.literal_eval(input_str)
dateStart=datetime.date(input_list[0],input_list[1],input_list[2])
dateEnd=datetime.date(input_list[3],input_list[4],input_list[5])
from dateutil.rrule import rrule, MONTHLY
months = [dt.strftime("%B") for dt in rrule(MONTHLY, dtstart=dateStart, until=dateEnd)]
print(months)
and provided the following input to above code from command prompt:
[2017,1,1,2017,2,4]
How can I extend my code to get the list of months where I need to pay the rent?
Sample input:
[2017,1,1,2017,1,1]
Sample Output:
['January']
my code Failed on the following case:
Input
[2017,8,2,2018,1,1]
Solution output
['August', 'September', 'October', 'November', 'December']
Expected output
['January', 'August', 'September', 'October', 'November', 'December']
Upvotes: 1
Views: 1407
Reputation: 151
Simplified Solution for asked problem:
# dt = [2017,3,1,2017,12,1]
dt = [2017,8,1,2018,7,1]
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
if dt[0] == dt[3]:
# print("Not here")
print(months[dt[1]-1:dt[4]])
else:
# print("here")
a=(months[0:dt[4]])
b=(months[dt[1]-1:13])
print(a+b)
Sample Output:
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
Upvotes: 1
Reputation: 29
date = [2017,8,1,2019,1,1]
months = ['January', 'Februry', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
count = 0
if date[0] == date[3]:
count = date[4] - date[1]
print(months[date[1]-1:date[4]])
else:
count_year = date[3] - date[0]
months = (count_year+1)*months
print(months[date[1]-1:date[4]+(count_year*12)])
Upvotes: 1
Reputation: 191844
The day is messing up the recurrence relation. Since you'd still pay rent in the middle of the month, the day is not important and can be ignored.
So, count the months, and then you could use count
rather than until
for rrule
For example,
y1 = input_list[0]
m1 = input_list[1]
y2 = input_list[3]
m2 = input_list[4]
if y1 > y2:
raise Error("first date after second")
elif y1 == y2:
if m1 == m2:
count = 0
elif m2 > m1:
count = m2 - m1
else:
raise Error("first date after second")
else:
count = (12 + m2) - m1
if count > 12:
raise Error("more than 12 month difference")
count += 1 # inclusive of the same month
import datetime
from dateutil.rrule import rrule, MONTHLY
date_start = datetime.date(y1, m1, 1)
months = [dt.strftime("%B") for dt in rrule(MONTHLY, dtstart=date_start, count=count)]
print(months)
Input: [2017,8,2,2018,1,1]
Output: ['August', 'September', 'October', 'November', 'December', 'January']
"sorted chronologically" is unclear if January should be at the very beginning, or after December
Upvotes: 2
Reputation: 886
The dateutil rrule
tests the recurrences of the start date up to and including the end date. In your failure case, the MONTHLY recurrences of 2017-8-5 are 2017-8-5, 2017-9-5, 2017-10-5, 2017-11-5 and 2017-12-5. 2018-1-5 is not counted since it's after the end date of 2018-1-1. To apply it to your problem, you need to force the start time day of the month to 1. [2017,8,1,2018,1,1]
will work correctly.
Upvotes: 1