Created
December 11, 2019 16:24
-
-
Save daxxog/72011d76ff4695dfb6221778d08ca727 to your computer and use it in GitHub Desktop.
Long division python script I made for my kid's math homework.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# long-div.py | |
# (c) 2019 David Volm (daXXog) <>< + + + ><> | |
# ___________________________________________ | |
firstNumber=input('whats the first number? \n') | |
secondNumber=input('whats the second number? \n') | |
def floorDiv(): | |
thinkFloor = str(int(secondNumber // firstNumber)) | |
lenDiff = len(str(secondNumber)) - len(thinkFloor) | |
return thinkFloor if lenDiff == 0 else ('0' * lenDiff) + thinkFloor | |
def modDiv(): | |
return str(int(secondNumber % firstNumber)) | |
def allignDigit(digit, expected, index): | |
isFirst = (index == 0) | |
return digit if isFirst else (((index - 1) * ' ') + ((expected - len(digit)) * ' ') + digit) | |
def solveSteps(): | |
fd = floorDiv() | |
sn = str(secondNumber) | |
index = 0 | |
carry = '' | |
spaceing = (len(str(firstNumber)) + 1) * ' ' | |
allignment = 0 | |
for next in fd: | |
topNumber = int(carry + sn[index]) | |
bottomNumber = int(next) * firstNumber | |
carry = str(topNumber - bottomNumber) | |
allignment = (len(str(firstNumber)) + 1) | |
if index != 0: | |
print(spaceing + allignDigit(str(topNumber), allignment, index)) | |
print(spaceing + allignDigit(str(bottomNumber), allignment, index)) | |
if index == (len(fd)-1): | |
print(spaceing + allignDigit(allignment * '-', allignment, index)) | |
print(spaceing + allignDigit(carry, allignment, index)) | |
print(spaceing + allignment * '___') | |
index += 1 | |
def printAnswer(): | |
print(((len(str(firstNumber)) + 1) * ' ') + floorDiv() + " r " + modDiv()) | |
def printProblem(): | |
lastLine = str(firstNumber) + '|' + str(secondNumber) | |
print(len(lastLine) * '_') | |
print(lastLine) | |
print('\nthe answer is -->') | |
printAnswer() | |
printProblem() | |
solveSteps() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did this changes, but the output is not expected, for example, 21 and 2.