Gusto Gusto - 1 year ago 178
Python Question

Return a list of weekdays

My task is to define a function

that returns a list of weekdays, starting with weekday. It should work like this:

>>> weekdays('Wednesday')
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

So far I've come up with this one:

def weekdays(weekday):
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
result = ""
for day in days:
if day == weekday:
result += day
return result

But this prints the input day only:

>>> weekdays("Sunday")

What am I doing wrong?

Answer Source
def weekdays(day):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    i=days.index(day) # get the index of the selected day
    d1=days[i:] #get the list from an including this index
    d1.extend(days[:i]) # append the list form the beginning to this index
    return d1

And if you want to test that it works:

def test_weekdays():
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    for day in days:
        print weekdays(day)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download