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 :Balance of the future
You wish to join the Time Brigade, an elite team composed only of meta-humans capable of seeing into the future. By gathering the best meta-humans, the Brigade is of great help in anticipating certain events that are harmful to Humanity and the Planet, foreseeing them and proposing optimal solutions even before they happen.
The Brigade is composed of 4 Orders: Foresight, Innovation, Wisdom and Protection. Today is the big day: after months of preparation, you will take the final exam to join the Order of Innovation. You will have to show that you are a meta-human, and one of the best.
You enter the test room where an officer awaits you, along with a gigantic map of the city. One of the missions of the Order of Innovation is to guarantee the energy supply to the vital functions of the city. Your test will be to predict disasters that could damage the network, and to examine the network.
Each line feeds 3 different areas of the city, and the load on each line must be roughly balanced between these 3 areas. Write a program that determines whether a given power line is balanced or not.
Data
Input
Line 1: a string of characters between 3 and 20 in length, representing the load on a given line. One character represents one unit of load. The load is distributed over 3 zones, represented by
A,BandC.Output
Your program should return on the standard output
YESif the line is balanced,NOotherwise. To be balanced, the number of loads on each zone (A, B and C) must differ by 2 units or less between each zone.Examples
Example 1
ABCABCABBOn this line, there are 3 loads on
A, 4 loads onBand 2 onC. There is therefore a maximum difference of 2 loads on the line (betweenBandC). This line is therefore balanced, so we must returnYES.Example 2
ABBBBCOn this line, there is a load difference of 3. You should therefore return
NO. Assistant: - Assistant
def is_balanced(line: str) -> str: # Compter le nombre d'occurrences des charges pour chaque zone count_A = line.count('A') count_B = line.count('B') count_C = line.count('C') # Récupérer le plus grand et le plus petit nombre d'occurrences max_load = max(count_A, count_B, count_C) min_load = min(count_A, count_B, count_C) # Vérifier si la différence est inférieure ou égale à 2 if max_load - min_load <= 2: return "YES" else: return "NO" # Lire l'entrée, supposer que l'entrée est passée directement sur la première ligne de l'entrée standard import sys input_line = sys.stdin.read().strip() print(is_balanced(input_line))