Sagot :
Réponse :
def decimalToBinary(decimalNumber):
binaryNumber = []
events = []
while decimalNumber != 0:
binaryNumber.append(decimalNumber % 2)
decimalNumber = int(decimalNumber / 2)
events.append((decimalNumber, "/2"))
events.append(tuple(reversed(binaryNumber)))
return events
def binaryToDecimal(binaryNumberDefault):
binaryNumber = tuple(int(c) for c in reversed(str(binaryNumberDefault)))
decimalNumber = 0
events = []
for i in range(0, len(binaryNumber)):
if binaryNumber[i] != 0:
decimalNumber += 2**i
events.append("ans + 2**" + str(i) + "=" + str(decimalNumber))
events.append(str(binaryNumberDefault) + "=" + str(decimalNumber))
return events
Explications :
Chacune des deux fonctions retourne un tableau contenant les étapes de conversion, le dernier élément du tableau est le résultat de conversion.