Skip to content

Instantly share code, notes, and snippets.

@daxxog
Created December 11, 2019 16:24
Show Gist options
  • Save daxxog/72011d76ff4695dfb6221778d08ca727 to your computer and use it in GitHub Desktop.
Save daxxog/72011d76ff4695dfb6221778d08ca727 to your computer and use it in GitHub Desktop.
Long division python script I made for my kid's math homework.
# 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()
@carlosal1015
Copy link

I did this changes, but the output is not expected, for example, 21 and 2.

5,7c5,6
< firstNumber = input("whats the first number? \n")
< secondNumber = input("whats the second number? \n")
< 
---
> firstNumber=input('whats the first number? \n')
> secondNumber=input('whats the second number? \n')
10,13c9,11
<     thinkFloor = str(int(secondNumber) // int(firstNumber))
<     lenDiff = len(str(secondNumber)) - len(thinkFloor)
<     return thinkFloor if lenDiff == 0 else ("0" * lenDiff) + thinkFloor
< 
---
> 	thinkFloor = str(int(secondNumber // firstNumber))
> 	lenDiff = len(str(secondNumber)) - len(thinkFloor)
> 	return thinkFloor if lenDiff == 0 else ('0' * lenDiff) + thinkFloor
16,17c14
<     return str(int(secondNumber) % int(firstNumber))
< 
---
> 	return str(int(secondNumber % firstNumber))
20,26c17,18
<     isFirst = index == 0
<     return (
<         digit
<         if isFirst
<         else (((index - 1) * " ") + ((expected - len(digit)) * " ") + digit)
<     )
< 
---
> 	isFirst = (index == 0)
> 	return digit if isFirst else (((index - 1) * ' ') + ((expected - len(digit)) * ' ') + digit)
29,50c21,39
<     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) * int(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
< 
---
> 	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
53,54c42
<     print(((len(str(firstNumber)) + 1) * " ") + floorDiv() + " r " + modDiv())
< 
---
> 	print(((len(str(firstNumber)) + 1) * ' ') + floorDiv() + " r " + modDiv())
57,60c45,47
<     lastLine = str(firstNumber) + "|" + str(secondNumber)
<     print(len(lastLine) * "_")
<     print(lastLine)
< 
---
> 	lastLine = str(firstNumber) + '|' + str(secondNumber)
> 	print(len(lastLine) * '_')
> 	print(lastLine)
62c49
< print("\nthe answer is -->")
---
> print('\nthe answer is -->')
65c52
< solveSteps()
---
> solveSteps()
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment