Return a list of weekdays, starting with given weekday

后端 未结 9 2328
情书的邮戳
情书的邮戳 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:35

    A far quicker approach would be to keep in mind, that the weekdays cycle. As such, we just need to get the first day we want to include the list, and add the remaining 6 elements to the end. Or in other words, we get the weekday list starting from the starting day, append another full week, and return only the first 7 elements (for the full week).

    days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
    def weekdays ( weekday ):
        index = days.index( weekday )
        return list( days[index:] + days )[:7]
    
    >>> weekdays( 'Wednesday' )
    ['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
    

提交回复
热议问题