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 :Best startup in France
For the Master Dev France after-party, you want to offer booths to the most innovative startups in France, so they can present their latest innovations. So you have
Nbooths lined up, which you have already assigned toNstartups. Each startup will present one of their two most recent innovations and you wonder if it seems likely that two adjacent startups will not present on the same theme.Given the themes of each startup's two recent innovations (given in the order of appearance of their booths), give the number of ways to choose an innovation per startup so that you never have the same theme twice in a row.
Data
Input
Line 1 : An integer
Nless than or equal to 60 corresponding to the number of startups.N following lines : Two alphanumeric strings separated by a space and describing the theme of each of the two innovations of the
i-ème startup.Output
The number of ways to choose an innovation per startup so that you never have the same theme twice in a row.
Example
For the input :
4 Ecommerce VirtualReality Fintech B2BSoftware Fintech Ecommerce Ecommerce EcommerceThe answer is:
2because the following choices can be made (each line describes a possibility):
Innovation 2 (VirtualReality) Innovation 1 (B2BSoftware) Innovation 1 (Fintech) Innovation 1 (Ecommerce) Innovation 2 (VirtualReality) Innovation 1 (B2BSoftware) Innovation 1 (Fintech) Innovation 2 (Ecommerce)For the input :
10 Ecommerce B2BSoftware Fintech Fintech VirtualReality Logistics Edtech AI AI Edtech RealEstate DeliveryServices AI Fintech VirtualReality Logistics HeathcareTech AI Entertainment BigDataThe answer is:
512Assistant:
- Assistant
def count_valid_innovation_combinations(n: int, themes: list[tuple[str, str]]) -> int: # Initialisation de la matrice de compatibilité avec des zéros dp = [[0, 0] for _ in range(n)] # Choisir le premier thème est toujours possible dp[0][0] = 1 dp[0][1] = 1 # Pour chaque startup, compter les façons valides de choisir un thème sans répéter celui de la startup précédente for i in range(1, n): # Si l'innovation actuelle est différente de la précédente, il peut recevoir le nombre de ways de la précédente if themes[i][0] != themes[i-1][0]: dp[i][0] += dp[i-1][0] if themes[i][0] != themes[i-1][1]: dp[i][0] += dp[i-1][1] if themes[i][1] != themes[i-1][0]: dp[i][1] += dp[i-1][0] if themes[i][1] != themes[i-1][1]: dp[i][1] += dp[i-1][1] # La solution est la somme des deux dernières positions possibles return dp[-1][0] + dp[-1][1] # Lecture de l'entrée import sys input_data = sys.stdin.read().strip().split('\n') # Extraction du nombre de startups n = int(input_data[0]) themes = [tuple(line.split()) for line in input_data[1:]] # Calculer et afficher le nombre de combinaisons valides print(count_valid_innovation_combinations(n, themes))