Return a list of weekdays, starting with given weekday

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

    Your result variable is a string and not a list object. Also, it only gets updated one time which is when it is equal to the passed weekday argument.

    Here's an implementation:

    import calendar
    
    def weekdays(weekday):
        days = [day for day in calendar.day_name]
        for day in days:
            days.insert(0, days.pop())    # add last day as new first day of list           
            if days[0] == weekday:        # if new first day same as weekday then all done
                break       
        return days
    

    Example output:

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

提交回复
热议问题