Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
def days_difference(day1, day2):
''' (int, int) -> int
Return the number of days from the first parameter to the second parameter, 
which are both in the range 1-365 (and thus indicate a day of the year).
'''
    return day2-day1

def get_weekday(d1, d2):
''' (int, int) -> int
The first parameter indicates the current day of the week, and is in the 
range 1-7. The second parameter indicates a number of days from the current 
day, and that could be any integer, including a negative integer. Return 
which day of the week it will be that many days from the current day.
'''
    weekday = (d1+d2) % 7 or 7
    return weekday

def get_birthday_weekday(d1, d2, d3):
''' (int, int, int) -> int
The first parameter indicates the current day of the week, and is in the 
range 1-7. The second parameter indicates the current day of the year, and 
is in the range 1-365. The third parameter indicates which day of the year 
a birthday falls on, and is also in the range 1-365. Return the day of the 
week it will be on that birthday (a number in the range 1-7). Hint: call 
the other two functions to do part of the work for you.
'''
    d = (days_difference(d2, d3))
    d_final = get_weekday(d1, d)
    return d_final

Hi, ive done these functions, but maybe could you guys check for any possible errors?

thank you

sunday is 1, monday is 2 and .... sat is 7

share|improve this question
Having other people look over your code is not constructive. What you should do is look into automated testing. – StoryTeller Jan 20 at 23:26
1  
You might also want less verbose doc strings – Jakob Bowyer Jan 20 at 23:36
-1 "Hint: call the other two functions to do part of the work for you." implies homework. – John Machin Jan 21 at 1:25
1  
Is there no calender library in python? – mnhg Jan 21 at 9:12

migrated from stackoverflow.com Jan 20 at 23:27

1 Answer

No-one would ever use any of these functions. For example, look at the specification for get_birthday_weekday. In order to call this function you need to know the day number of the year for someone's birthday. But birthdays fall on a calendar day, that is, on a particular day of a particular month. The only plausible way in which you would know the day number of the year for a birthday would be if you had just computed it:

>>> from datetime import date
>>> birthday = date(2013, 12, 1)
>>> birthday.timetuple()[7]
335

But in such a case, if you wanted the weekday, you'd compute it directly:

>>> birthday.strftime('%A')
'Sunday'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.