- Change your integer into a string. Let us call this
alpha
- Find the
length
of alpha
- Create two empty string:
beta
and gamma
- Init a
for-loop
from 0 to length
Add all elements in alpha
to beta
until a .
is found
If a decimal is not found or only 0 (or any amounts of zeros), then your number is over `alpha` over 1.
If a decimal is found, start adding the elements to `gamma`
Convert gamma
back into an int
and use the function you found previously.
- Convert
beta
back into an int
and multiply this by gamma
.
- Add this to the tuple from the function you posted.
All done!
I wrote this in python to quickly demonstrate an implementation.
PLEASE NOTE THAT THIS IS NOT DONE AS WE ARE NOT SIMPLIFYING THE FRACTION. THIS ALSO DOES NOT USE THE PATTERN THAT I LISTED ABOVE BECAUSE OF REASONS paul manta DECLARED.
Here ya go,
def findFraction(string):
from math import pow
length = len(string)
c = '{a}/{b}'.format(a=string, b=int(pow(10,length)))
print(c)
return c
def finishFraction(addToNumerator, currentFraction):
temp = ''
temp2 = ''
slashFlag = False
for i in range(0,len(currentFraction)):
if currentFraction[i]=='/':
slashFlag = True
elif slashFlag==True:
temp2 =temp2+currentFraction[i]
else:
temp = temp+currentFraction[i]
numerator = int(temp)+(int(addToNumerator)*int(temp2))
fraction = str(numerator)+'/'+str(temp2)
return fraction
def fractionForm(number):
alpha = str(number)
length = len(alpha)
beta = ''
gamma = ''
dotFlag = False
for i in range(0, length):
if alpha[i]=='.':
if dotFlag==True:
print("SHOULD NOT HAVE TWO DECIMALS IN NUMBER")
assert(False)
dotFlag=True
elif dotFlag==False:
beta=beta+alpha[i]
else:
gamma=gamma+alpha[i]
if gamma=='':
print('{a}/{b}'.format(a=int(beta),b=1))
return (int(beta),1)
else:
fraction = findFraction(gamma)
fraction = finishFraction(beta,fraction)
print(fraction)
return fraction
def test():
woooo = fractionForm(1.25)
woohoo= fractionForm(99.999)
dtest()