Python Forum
Task calendar problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Task calendar problem
#1
Here is the task:
Analyzing weekends and holidays around New Year's and May 1st in Russia, the president of Flatlandia has concluded that the rest of its citizens can be drastically optimized. The main goal is to ensure that citizens do not have to work more than 6 consecutive days in a calendar year. The president has instructed the Ministry of Labor to develop a schedule for transferring weekends (Saturdays and Sundays) so that citizens can have as many consecutive rest days as possible.

It is important to note that if a Flatlandian public holiday falls on a weekend (Saturday or Sunday), that holiday automatically moves to the first working day after the holiday. However, according to the president's decree, any weekend, whether it coincides with a public holiday or not, can be moved to any working day. Public holidays, however, are never moved.

Write a program that will help the Ministry of Labor create the required schedule for transferring weekends in the upcoming year. Public holidays and weekends from the previous and following year should not be considered. The objective is to maximize the number of consecutive rest days in one year.

Input Format:

The first line of input contains two numbers - the year number (Y) (2012≤Y≤2050) and the day of the week for January 1st of that year (W) (1≤W≤7, from Monday to Sunday). In this range of years, leap years are divisible by 4.

The second line contains the number of annual public holidays (N) in Flatlandia. Each of the following N lines contains the date of the next holiday in the format DD.MM. The holiday dates are listed in chronological order, and all dates are valid and correct for the given year.

Output Format:

Output a single number - the maximum possible number of consecutive days off for the residents of Flatlandia in the specified year if the weekends are moved so that the number of consecutive working days in this year does not exceed 6.

Example:

Input:

2012 7

1

01.01

Output:

63


My try:
import datetime

def is_leap_year(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

def get_weekday(year, month, day):
    date = datetime.date(year, month, day)
    return date.weekday()

def get_weekdays_in_year(year):
    weekdays = [0, 0, 0, 0, 0, 0, 0]  # Sunday to Saturday
    for month in range(1, 13):
        days_in_month = 31
        if month == 4 or month == 6 or month == 9 or month == 11:
            days_in_month = 30
        elif month == 2:
            if is_leap_year(year):
                days_in_month = 29
            else:
                days_in_month = 28
        for day in range(1, days_in_month + 1):
            weekday = get_weekday(year, month, day)
            weekdays[weekday] += 1
    return weekdays

def get_maximum_consecutive_holidays(year, start_weekday, holidays):
    weekdays = get_weekdays_in_year(year)
    consecutive_holidays = 0
    current_consecutive_holidays = 0
    for holiday in holidays:
        day, month = map(int, holiday.split('.'))
        weekday = get_weekday(year, month, day)
        if weekdays[weekday] == 0:  # Weekday is not already a holiday
            current_consecutive_holidays += 1
        else:
            current_consecutive_holidays = 0
        weekdays[weekday] += 1
        if current_consecutive_holidays > consecutive_holidays:
            consecutive_holidays = current_consecutive_holidays
    return consecutive_holidays


year, start_weekday = map(int, input().split())
num_holidays = int(input())
holidays = []
for _ in range(num_holidays):
    holiday = input()
    holidays.append(holiday)

maximum_consecutive_holidays = get_maximum_consecutive_holidays(year, start_weekday, holidays)


print(maximum_consecutive_holidays)
But uhhh, it doesn't complete the first test, help plzz
Yah its about russia, so sorry..
Reply


Messages In This Thread
Task calendar problem - by humanical - Aug-30-2023, 07:50 PM
RE: Task calendar problem - by Pedroski55 - Aug-31-2023, 06:08 AM
RE: Task calendar problem - by humanical - Aug-31-2023, 07:35 AM
RE: Task calendar problem - by Pedroski55 - Aug-31-2023, 02:14 PM
RE: Task calendar problem - by humanical - Aug-31-2023, 03:13 PM
RE: Task calendar problem - by Pedroski55 - Sep-01-2023, 05:17 AM
RE: Task calendar problem - by Pedroski55 - Sep-02-2023, 10:19 AM
RE: Task calendar problem - by humanical - Sep-04-2023, 04:08 AM
RE: Task calendar problem - by Pedroski55 - Sep-04-2023, 02:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calendar program louienyy 2 6,227 Mar-30-2020, 01:21 PM
Last Post: louienyy
Sad [Learning] Calendar without modules or list KoFu 5 6,992 Sep-09-2019, 03:25 PM
Last Post: DeaD_EyE
  Calendar calculations frequency 10 7,361 Nov-13-2018, 07:34 PM
Last Post: frequency
  Calendar calcualtions deusvult 11 10,194 May-01-2017, 02:56 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020