1

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"
7
  • Please show what you have tried so far. Commented Apr 4, 2015 at 2:20
  • Have you tried using datetime and timedelta from the datetime module? Commented Apr 4, 2015 at 2:21
  • I haven't learned either of those yet - I'm in an intro to python class. This is what I have so far: 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" Commented Apr 4, 2015 at 2:21
  • Can you edit your original question and put the code in there? Just copy-paste it in and then highlight it and press control+K or press the button that looks like { }. Commented Apr 4, 2015 at 2:23
  • @Nora, code in comments is unreadable due to no formatting -- please copy that code into an edit of your question (and format it appropriately by highlighting it and clicking the {} icon!-) Commented Apr 4, 2015 at 2:25

4 Answers 4

2

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!-)

Sign up to request clarification or add additional context in comments.

7 Comments

am trying to go through this now!
@Shashank oops, you're right, thanks, editing to fix the "mental typo":-).
still not working... any way I could modify my code up top (I know it's missing some bits) and make it work? sorry I am very new to python and struggling a lot!
@Nora, have you tried with the current version (using index as Shashank suggested, though it seems his comment's gone, instead of find)?
@Nora now try making the weirdly-named number, which is actually a string, into in fact a number, as per my latest edit...!-)
|
0

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

Comments

0

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)]

Comments

0
# 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.