Question

Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays...

Language: PYTHON

Function name : favorite_day
Parameters : list of tuples (dates), int (weekday, 0-6, Mondays are 0),
int (day of the month 1 to 28)
Returns: dates: list of tuples
Description: Imagine that you have a favorite weekday, and want to see if certain days fall on that weekday. Using the calendar module from the Python standard library , write a function which takes in a list of tuples formatted like [(month, year), etc.], your favorite weekday, and a day of the month. Using the module, find the weekday of each (month,year) tuple at the day of the month that was passed in (1-28). If that weekday is equal to your favorite weekday, add the tuple to a list to be returned at the end of your code. Assume the day of the week will lie within 0-6 inclusive and the day of the month will be always be valid.

(Hint: look at calendar.weekday method)

Test Cases:

>>> dates = [(1, 1999), (7, 1980), (3, 2018), (12, 2003)] >>> print(favorite_day(dates, 6, 20))
[(7, 1980)]

>>> dates = [(10, 1803), (1, 2019), (6, 1964), (11, 1920), (2, 2011)] >>> print(favorite_day(dates, 0, 1))
[(6, 1964), (11, 1920)]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#look for the indentation

CODE:

import calendar


def favorite_day(dates, week_day, day):
    match_weekday = []
    # looing on the list
    # i, j have the values in months, years

    for i, j in dates:
        #calendar.weekday(year, month)
        if calendar.weekday(j, i, day) == week_day:
            match_weekday.append((i, j))
    # return the list
    return match_weekday


dates = [(10, 1803), (1, 2019), (6, 1964), (11, 1920), (2, 2011)]
print(favorite_day(dates, 0, 1))

# dates = [(1, 1999), (7, 1980), (3, 2018), (12, 2003)]
# print(favorite_day(dates, 6, 20))
# [(7, 1980)]

Add a comment
Know the answer?
Add Answer to:
Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT