affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Olympic games - What season?
Because of climate change, to Vivaldi's great misfortune, there are no longer 4 seasons but N different seasons. The i-th season lasts for J_i days, naturally, after the end of the N-th season, we return to the first season.
We are currently at the first day of the first season. You work for the city hall of Paris which wants to organize the Olympics on the K-th day starting today. You want to know what the season will be like on that day to see if it is possible to organize the Olympics there.
Data
Input
Line 1 : An integer N ( 1 <= N <= 10^5 ), the number of seasons.
Line 2: An integer K (1 <= K <= 10^9), the day whose season you want to know.
Line 3 : N integers, where the i-th integer is the number of days in the i-th season
Output
Only one integer: The number of the season after K days.
Example
For the input :
5 17 10 20 5 18 12The season is :
2Indeed, the first season lasts from day 1 to day 10, while the second one lasts from day 11 to day 30. So the 17th day is during the second season.
For the input :
5 237 10 20 5 18 12The season is :
4Indeed, the 237th day is on the 7th day of the 4th season. Assistant:
- Assistant
def what_season(N: int, K: int, seasons: list[int]) -> int: # Calculer le nombre total de jours dans un cycle complet de saisons cycle_length = sum(seasons) # Calculer le jour cible dans le cycle target_day = K % cycle_length # Si le jour cible est zéro, cela signifie que c'est le dernier jour du cycle if target_day == 0: target_day = cycle_length # Variables pour parcourir les saisons day_count = 0 # Parcourir les saisons pour trouver la saison actuelle for i in range(N): day_count += seasons[i] if target_day <= day_count: return i + 1 # Lecture des données d'entrée import sys input = sys.stdin.read data = input().split() # Nombre de saisons N = int(data[0]) # Jour cible K = int(data[1]) # Jours pour chaque saison seasons = list(map(int, data[2:])) # Afficher la saison à laquelle appartient le jour K print(what_season(N, K, seasons))