mirror of
https://github.com/DeaDvey/mathgenerator.git
synced 2025-11-28 14:35:23 +01:00
Merge pull request #231 from devansh-07/master
Add 2's complement of a binary number
This commit is contained in:
@@ -75,3 +75,4 @@ from .euclidianNormFunc import *
|
|||||||
from .angleBtwVectorsFunc import *
|
from .angleBtwVectorsFunc import *
|
||||||
from .absoluteDifferenceFunc import *
|
from .absoluteDifferenceFunc import *
|
||||||
from .vectorDotFunc import *
|
from .vectorDotFunc import *
|
||||||
|
from .binary2sComplement import *
|
||||||
|
|||||||
26
mathgenerator/funcs/binary2sComplement.py
Normal file
26
mathgenerator/funcs/binary2sComplement.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from .__init__ import *
|
||||||
|
|
||||||
|
def binary2sComplementFunc(maxDigits=10):
|
||||||
|
digits = random.randint(1, maxDigits)
|
||||||
|
question = ''.join([str(random.randint(0, 1)) for i in range(digits)]).lstrip('0')
|
||||||
|
|
||||||
|
answer = []
|
||||||
|
for i in question:
|
||||||
|
answer.append(str(int(not bool(int(i)))))
|
||||||
|
|
||||||
|
carry = True
|
||||||
|
j = len(answer) - 1
|
||||||
|
while j >= 0:
|
||||||
|
if answer[j] == '0':
|
||||||
|
answer[j] = '1'
|
||||||
|
carry = False
|
||||||
|
break
|
||||||
|
answer[j] = '0'
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
if j == 0 and carry == True:
|
||||||
|
answer.insert(0, '1')
|
||||||
|
|
||||||
|
problem = "2's complement of " + question + " ="
|
||||||
|
solution = ''.join(answer).lstrip('0')
|
||||||
|
return problem, solution
|
||||||
@@ -160,3 +160,4 @@ eucldianNorm=Generator("Euclidian norm or L2 norm of a vector", 69, "Euclidian N
|
|||||||
angleBtwVectors=Generator("Angle between 2 vectors", 70, "Angle Between 2 vectors V1=[v11, v12, ..., v1n] and V2=[v21, v22, ....., v2n]", "V1.V2 / (euclidNorm(V1)*euclidNorm(V2))", angleBtwVectorsFunc)
|
angleBtwVectors=Generator("Angle between 2 vectors", 70, "Angle Between 2 vectors V1=[v11, v12, ..., v1n] and V2=[v21, v22, ....., v2n]", "V1.V2 / (euclidNorm(V1)*euclidNorm(V2))", angleBtwVectorsFunc)
|
||||||
absoluteDifference=Generator("Absolute difference between two numbers", 71, "Absolute difference betweeen two numbers a and b =", "|a-b|", absoluteDifferenceFunc)
|
absoluteDifference=Generator("Absolute difference between two numbers", 71, "Absolute difference betweeen two numbers a and b =", "|a-b|", absoluteDifferenceFunc)
|
||||||
vectorDot = Generator("Dot Product of 2 Vectors", 72, "a . b = ", "c", vectorDotFunc)
|
vectorDot = Generator("Dot Product of 2 Vectors", 72, "a . b = ", "c", vectorDotFunc)
|
||||||
|
binary2sComplement = Generator("Binary 2's Complement", 73, "2's complement of 11010110 =", "101010", binary2sComplementFunc)
|
||||||
|
|||||||
Reference in New Issue
Block a user