Return a list of weekdays, starting with given weekday

后端 未结 9 2325
情书的邮戳
情书的邮戳 2021-02-07 23:53

My task is to define a function weekdays(weekday) that returns a list of weekdays, starting with the given weekday. It should work like this:

9条回答
  •  臣服心动
    2021-02-08 00:19

    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)
    

提交回复
热议问题