Files
mathgenerator/mathgenerator/funcs/computer_science/bcd_to_decimal.py
Luke Weiler 20d3294a93 Remove asterisk imports from subject inits (#391)
* removed asterisk imports from subject inits

* import Generator from the right init level
2022-12-19 02:06:10 -05:00

31 lines
823 B
Python

from ...__init__ import Generator
import random
def gen_func(maxNumber=10000, format='string'):
n = random.randint(1000, maxNumber)
binstring = ''
while True:
q, r = divmod(n, 10)
nibble = bin(r).replace('0b', "")
while len(nibble) < 4:
nibble = '0' + nibble
binstring = nibble + binstring
if q == 0:
break
else:
n = q
if format == 'string':
problem = "Integer of Binary Coded Decimal " + str(n) + " is = "
solution = int(binstring, 2)
return problem, solution
elif format == 'latex':
return "Latex unavailable"
else:
return n, int(binstring, 2)
bcd_to_decimal = Generator("Binary Coded Decimal to Integer", 91,
gen_func, ["maxNumber=10000"])