nh919
nh919

Reputation: 27

Adding days to a current date python

Write a function that helps answer questions like '"Today is Wednesday. I leave on holiday in 19 days time. What day will that be?"' ...So the function must take a day name and a delta argument — the number of days to add — and should return the resulting day name:

def day_add(day, number):
    if number == "0":
        return day
    else:
        return

result = day_add("Thursday", "0")
print(result)

assert "Friday" == day_add("Monday", "4"), "gives you back the day it will be if you add a certain number of days"
assert "Tuesday" == day_add("Tuesday", "0"), "gives back the day you put in"

Upvotes: 1

Views: 1929

Answers (4)

Alessandro Anderson
Alessandro Anderson

Reputation: 21

# Write the function day_name which is given a number, and returns its 
# name:

def day_name(number):
    if number == 0:
        return ('Sunday')
    elif number == 1:
        return ('Monday')
    elif number == 2:
        return ('Tuesday')
    elif number == 3:
        return ('Wednesday')
    elif number == 4:
        return ('Thursday')
    elif number == 5:
        return ('Friday')
    elif number == 6:
        return ('Saturday')
    else:
        return None

# Write the inverse function day_num which is given a day name, and returns its    # number:


def day_num(day_name):  
    if day_name == 'Sunday':
        return (0)
    elif day_name == 'Monday':
        return (1)
    elif day_name == 'Tuesday':
        return (2)
    elif day_name == 'Wednesday':
        return (3)
    elif day_name == 'Thursday':
        return (4)
    elif day_name == 'Friday':
        return (5)
    elif day_name == 'Saturday':
        return (6)
    else:
        return None

# Use composition to write the last function
# If Sunday i'm leaving for 10 days, i will stay 1 week and 3 days. So the day 
# i'll be back will be 3 days from Sunday (Wednesday). 
# In code: day_back = (10 % 7) + (0)

def day_add(today, stay):
    today = day_num(today)
    day_back = (stay % 7) + today
    result = day_name(day_back)
    return (result)

Upvotes: 0

GrumpyBOFH
GrumpyBOFH

Reputation: 51

Start with the list of days:

DAYS=['Sunday', 'Monday', ..., 'Saturday']

You can use the index method of a list to find out the array index location for a provided day:

>>> print DAYS.index('Sunday')
0
>>> print DAYS.index('Monday')
1

Add to that the number of days from now and you will get the day at that index position in the DAYS list:

>>> todays_index=DAYS.index('Sunday')
>>> days_from_now=3
>>> print DAYS[todays_index+days_from_now]
Wednesday

This will, however, work for up to 6 days from Sunday. Setting days_from_now to 7 will obviously break things and cause an IndexError. However, if you calculate the modulus 7 of days_from_now, you can ensure that you'll never exceed 6:

# DAYS[0] == 'Sunday'
# 7 days from now: DAYS[0+(7%7)] == DAYS[0+0] == DAYS[0] == 'Sunday'
# 8 days from now: DAYS[0+(8%7)] == DAYS[0+1] == DAYS[1] == 'Monday'
# 14 days from now: DAYS[0+(14%7)] == DAYS[0+0] == DAYS[0] == 'Saturday'
# DAYS[1] == 'Monday'
# 14 days from now: DAYS[1+(14%7)] == DAYS[1+0] == DAYS[1] == 'Monday'
# 17 days from now: DAYS[1+(17%7)] == DAYS[1+3] == DAYS[4] == 'Thursday'

The resulting function would look like:

def day_add(day, days_from_now):
    DAYS=['Sunday', 'Monday', ..., 'Saturday']
    if days_from_now == 0:
        return day
    else:
        todays_index = DAYS.index(day)
        return DAYS[todays_index+(days_from_now%7)]

Upvotes: 0

dawg
dawg

Reputation: 103714

You could make an index of the days in a dictionary:

>>> dow={day:i for i, day in enumerate(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'])}

And an inverse of that:

>>> idow={i:day for day, i in dow.items()}

So now you have:

>>> dow
{'Monday': 1, 'Tuesday': 2, 'Friday': 5, 'Wednesday': 3, 'Thursday': 4, 'Sunday': 0, 'Saturday': 6}
>>> idow
{0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'}

Then you can get one from the other:

>>> idow[(dow['Wednesday']+19)%7]
Monday

Upvotes: 0

Alex Martelli
Alex Martelli

Reputation: 881497

Clearly you need to translate the day name into a number, e.g with a global list:

DAYS = ['Sunday', 'Monday', ... , 'Saturday']

(replace the ... with the other day names:-).

To translate the day name into an index in the list (a number from 0 to 6 included), use the list method index:

daynum = DAYS.index(dayname)

that raises a ValueError if dayname is not a valid weekday name, which I'm guessing is OK or else you would have told us your specifications for such a user error! (I'm assuming dayname is the name of the argument your function accepts).

Next, you add number to the daynum and take it modulo 7 so it's again a number between 0 and 6 included:

result_day = (daynum + number) % 7

finally, you use this to index the list and return the result:

return DAYS[result_day]

I hope you can put these together into the function you need so you get at least a little learning out of the exercise (as opposed to none if we gave you the needed function ready to copy and paste!-)

Edit: I see the number you're passing is actually a string -- any reason for this very peculiar choice...? If it's part of a really, truly, very remarkably strange specification, you'll also need to make into a number, of course -- i.e, at the very start,

number = int(number)

A suggestion to preserve the sanity of anybody reading your code: do not name number a variable that is not a number -- like the well-known psychological test where you have a bunch of color names each printed in a color different from the one it's naming, this kind of thing really throws people's brains for a loop!-)

Upvotes: 2

Related Questions