mirror of
https://github.com/DeaDvey/mathgenerator.git
synced 2025-11-28 14:35:23 +01:00
add format kwarg
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def BCDtoDecimalFunc(maxNumber=10000):
|
||||
def BCDtoDecimalFunc(maxNumber=10000, format='string'):
|
||||
n = random.randint(1000, maxNumber)
|
||||
binstring = ''
|
||||
while True:
|
||||
@@ -15,9 +15,12 @@ def BCDtoDecimalFunc(maxNumber=10000):
|
||||
else:
|
||||
n = q
|
||||
|
||||
problem = "Integer of Binary Coded Decimal " + str(n) + " is = "
|
||||
solution = int(binstring, 2)
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
problem = "Integer of Binary Coded Decimal " + str(n) + " is = "
|
||||
solution = int(binstring, 2)
|
||||
return problem, solution
|
||||
else:
|
||||
return n, int(binstring, 2)
|
||||
|
||||
|
||||
bcd_to_decimal = Generator("Binary Coded Decimal to Integer", 91,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def binary2sComplementFunc(maxDigits=10):
|
||||
def binary2sComplementFunc(maxDigits=10, format='string'):
|
||||
digits = random.randint(1, maxDigits)
|
||||
question = ''.join([str(random.randint(0, 1))
|
||||
for i in range(digits)]).lstrip('0')
|
||||
@@ -23,9 +23,12 @@ def binary2sComplementFunc(maxDigits=10):
|
||||
if j == 0 and carry is True:
|
||||
answer.insert(0, '1')
|
||||
|
||||
problem = "2's complement of " + question + " ="
|
||||
solution = ''.join(answer).lstrip('0')
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
problem = "2's complement of " + question + " ="
|
||||
solution = ''.join(answer).lstrip('0')
|
||||
return problem, solution
|
||||
else:
|
||||
return question, answer
|
||||
|
||||
|
||||
binary_2s_complement = Generator("Binary 2's Complement", 73,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def binaryComplement1sFunc(maxDigits=10):
|
||||
def binaryComplement1sFunc(maxDigits=10, format='string'):
|
||||
question = ''
|
||||
answer = ''
|
||||
|
||||
@@ -10,9 +10,11 @@ def binaryComplement1sFunc(maxDigits=10):
|
||||
question += temp
|
||||
answer += "0" if temp == "1" else "1"
|
||||
|
||||
problem = question + "="
|
||||
solution = answer
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
problem = question + "="
|
||||
return problem, answer
|
||||
else:
|
||||
return problem, answer
|
||||
|
||||
|
||||
binary_complement_1s = Generator("Binary Complement 1s", 4,
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def binaryToDecimalFunc(max_dig=10):
|
||||
def binaryToDecimalFunc(max_dig=10, format='string'):
|
||||
problem = ''
|
||||
|
||||
for i in range(random.randint(1, max_dig)):
|
||||
temp = str(random.randint(0, 1))
|
||||
problem += temp
|
||||
|
||||
solution = int(problem, 2)
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
solution = int(problem, 2)
|
||||
return problem, solution
|
||||
else:
|
||||
return problem, solution
|
||||
|
||||
|
||||
binary_to_decimal = Generator("Binary to Decimal", 15,
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def binaryToHexFunc(max_dig=10):
|
||||
def binaryToHexFunc(max_dig=10, format='string'):
|
||||
problem = ''
|
||||
for i in range(random.randint(1, max_dig)):
|
||||
temp = str(random.randint(0, 1))
|
||||
problem += temp
|
||||
|
||||
solution = hex(int(problem, 2))
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
solution = hex(int(problem, 2))
|
||||
return problem, solution
|
||||
else:
|
||||
return problem, solution
|
||||
|
||||
|
||||
binary_to_hex = Generator("Binary to Hexidecimal", 64, binaryToHexFunc,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def DecimalToBCDFunc(maxNumber=10000):
|
||||
def DecimalToBCDFunc(maxNumber=10000, format='string'):
|
||||
n = random.randint(1000, maxNumber)
|
||||
x = n
|
||||
# binstring = ''
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def DecimalToBinaryFunc(max_dec=99):
|
||||
def DecimalToBinaryFunc(max_dec=99, format='string'):
|
||||
a = random.randint(1, max_dec)
|
||||
b = bin(a).replace("0b", "")
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def deciToHexaFunc(max_dec=1000):
|
||||
def deciToHexaFunc(max_dec=1000, format='string'):
|
||||
a = random.randint(0, max_dec)
|
||||
b = hex(a)
|
||||
problem = "Binary of " + str(a) + "="
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def decimalToOctalFunc(maxDecimal=4096):
|
||||
def decimalToOctalFunc(maxDecimal=4096, format='string'):
|
||||
x = random.randint(0, maxDecimal)
|
||||
problem = "The decimal number " + str(x) + " in Octal is: "
|
||||
solution = oct(x)
|
||||
return problem, solution
|
||||
|
||||
if format == 'string':
|
||||
return problem, solution
|
||||
else:
|
||||
return x, oct(x)
|
||||
|
||||
|
||||
decimal_to_octal = Generator("Converts decimal to octal", 84,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def fibonacciSeriesFunc(minNo=1):
|
||||
def fibonacciSeriesFunc(minNo=1, format='string'):
|
||||
n = random.randint(minNo, 20)
|
||||
|
||||
def createFibList(n):
|
||||
@@ -16,9 +16,11 @@ def fibonacciSeriesFunc(minNo=1):
|
||||
|
||||
fibList = createFibList(n)
|
||||
|
||||
problem = "The Fibonacci Series of the first " + str(n) + " numbers is ?"
|
||||
solution = fibList
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
problem = "The Fibonacci Series of the first " + str(n) + " numbers is ?"
|
||||
return problem, fibList
|
||||
else:
|
||||
return n, fibList
|
||||
|
||||
|
||||
fibonacci_series = Generator(
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
from .__init__ import *
|
||||
|
||||
|
||||
def moduloFunc(maxRes=99, maxModulo=99):
|
||||
def moduloFunc(maxRes=99, maxModulo=99, format='string'):
|
||||
a = random.randint(0, maxModulo)
|
||||
b = random.randint(0, min(maxRes, maxModulo))
|
||||
c = a % b if b != 0 else 0
|
||||
|
||||
problem = str(a) + "%" + str(b) + "="
|
||||
solution = str(c)
|
||||
return problem, solution
|
||||
if format == 'string':
|
||||
problem = str(a) + "%" + str(b) + "="
|
||||
solution = str(c)
|
||||
return problem, solution
|
||||
else:
|
||||
return a, b, c
|
||||
|
||||
|
||||
modulo_division = Generator("Modulo Division", 5, moduloFunc,
|
||||
|
||||
@@ -3,13 +3,17 @@ from .__init__ import *
|
||||
import math
|
||||
|
||||
|
||||
def nthFibonacciNumberFunc(maxN=100):
|
||||
def nthFibonacciNumberFunc(maxN=100, format='string'):
|
||||
golden_ratio = (1 + math.sqrt(5)) / 2
|
||||
n = random.randint(1, maxN)
|
||||
problem = f"What is the {n}th Fibonacci number?"
|
||||
ans = round((math.pow(golden_ratio, n) - math.pow(-golden_ratio, -n)) / (math.sqrt(5)))
|
||||
solution = f"{ans}"
|
||||
return problem, solution
|
||||
|
||||
if format == 'string':
|
||||
solution = f"{ans}"
|
||||
return problem, solution
|
||||
else:
|
||||
return n, ans
|
||||
|
||||
|
||||
nth_fibonacci_number = Generator("nth Fibonacci number", 62,
|
||||
|
||||
Reference in New Issue
Block a user