Blogdemaths

Blogdemaths - Fonction base 13 de Conway

Mar 24th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. ########################################################
  2. #
  3. # blogdemaths.wordpress.com -
  4. #
  5. # Conway et la réciproque du théorème des valeurs intermédiaires
  6. #
  7. # https://blogdemaths.wordpress.com/2018/03/25/conway-et-la-reciproque-du-theoreme-des-valeurs-intermediaires/
  8. #
  9. ########################################################
  10.  
  11.  
  12. # Ce programme renvoie l'image d'un nombre écrit en base 13 par la fonction de Conway
  13.  
  14. import re
  15.  
  16. def fonction_Conway(nombre):
  17.     """ nombre: chaine de caractère représentant un nombre à virgule en base 13 """
  18.    
  19.     pattern1 = r'^[0-9A-C]*,{1}[0-9A-C]*A[0-9]*C[0-9]*$'
  20.     pattern2 = r'^[0-9A-C]*,{1}[0-9A-C]*B[0-9]*C[0-9]*$'
  21.    
  22.     if re.search(pattern1, nombre):
  23.         pattern = r'A[0-9]*C[0-9]*$'
  24.         match = re.findall(pattern, nombre)[0]
  25.         return match[1:].replace("C",",")
  26.    
  27.     elif re.search(pattern2, nombre):
  28.         pattern = r'B[0-9]*C[0-9]*$'
  29.         match = re.findall(pattern, nombre)[0]
  30.         return "-" + match[1:].replace("C",",")
  31.    
  32.     else:
  33.         return "0"
  34.        
  35.        
  36. if __name__ == "__main__":
  37.  
  38.     nombre = input("Entrer un nombre à virgule écrit en base 13:\n")
  39.    
  40.     if re.search(r'^[0-9A-C]*,{1}[0-9A-C]*$', nombre):
  41.         print("Son image par la fonction base 13 de Conway est:")
  42.         print(fonction_Conway(nombre))
  43.     else:
  44.         print("Le nombre entré n'est pas correct.")
Add Comment
Please, Sign In to add comment