def nth_day_of_month(n, weekday, month, year):
if not (<NUM_LIT:0> <= n <= <NUM_LIT:5>):<EOL><INDENT>raise IndexError("<STR_LIT>".format(n))<EOL><DEDENT>if not (<NUM_LIT:0> <= weekday <= <NUM_LIT:6>):<EOL><INDENT>raise IndexError("<STR_LIT>")<EOL><DEDENT>firstday, daysinmonth = calendar.monthrange(year, month)<EOL>first_weekday_of_kind = <NUM_LIT:1> + (weekday - firstday) % <NUM_LIT:7><EOL>if n == <NUM_LIT:0>:<EOL><INDENT>if first_weekday_of_kind in [<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>] and first_weekday_of_kind + <NUM_LIT> < daysinmonth:<EOL><INDENT>n = <NUM_LIT:5><EOL><DEDENT>else:<EOL><INDENT>n = <NUM_LIT:4><EOL><DEDENT><DEDENT>day = first_weekday_of_kind + ((n - <NUM_LIT:1>) * <NUM_LIT:7>)<EOL>if day > daysinmonth:<EOL><INDENT>raise IndexError("<STR_LIT>".format(n, month))<EOL><DEDENT>return (year, month, day)<EOL>
Return (year, month, day) tuple that represents nth weekday of month in year.
If n==0, returns last weekday of month. Weekdays: Monday=0