2019-2020/Formations/NSI/Bloc1/diubloc1-files-td5/change_repr.py

41 lines
878 B
Python

#! /usr/bin/env python3
"""
python3 demo_test2.py
"""
__author__ = "Laure Gonnord"
__copyright__ = "Univ Lyon1, 2019"
## fortement inspiré de http://sdz.tdct.org/sdz/du-decimal-au-binaire.html
CHIFFRES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
NOMBRES = {c: n for (n, c) in enumerate(CHIFFRES)}
# Fonctions utilitaires
def chiffre(n):
return CHIFFRES[n]
def nombre(ch):
"""ch= caractère qui désigne le nombre
retourne le nombre associé"""
return NOMBRES[ch]
chiffre(12) # 'C'
nombre('C') # 12
# base b vers décimal
def lire_repr(rep, b):
nb_chiff = len(rep)
somme = 0
# TODO
return somme
print(lire_repr('2A',16))
# représentation du nombre n en base b, retourne une chaîne
# à chaque fois on ajoute à la fin de la chaîne.
def repr_nombre(n, b):
pass
print(repr_nombre(10, 2)) # 1010
print(repr_nombre(42, 16)) # 2A