I have been learning Python for a while (my first programming language) in my spare time. Since it's been 20 years since math class, and I didn't get very far with that, I just brushed up on pre-algebra and have started learning algebra. I decided to try and write some scripts/functions for what I have been learning, and below is my attempt at writing a script to simplify fractions.
In this script I first check if the fraction given contains a '0', a '1' or if the numerator == denominator. Failing those test, I move on to finding multiples to do the simplification.
A few things that I am wondering:
- Is the main function just too long? Should I have broken it into smaller functions?
- In the first if block in
simplify_fraction()
fraction the 'else' part ends with 'pass'. Wasn't sure what else to do there? Or if that is fine as is? - Since I use the
abs()
function numerous times fornumer
anddenom
variable, would I have been better to make a variable out of it so that the calculation is only done once? - Anything else that don't look so great, or could be done better!
Also, I am aware I could create a function for user_input and add error checking to that if the user puts in anything else other than an integer, but I am not concerning myself with that for now.
This script is designed to be run through the command line.
def neg_or_pos(numer, denom):
if numer < 0 and denom > 0:
return True
elif numer > 0 and denom < 0:
return True
else:
return False
def simplify_fraction(numer, denom):
#assess if fraction is negative
negative = neg_or_pos(numer, denom)
simplified_statement = ("%d/%d is already at its most simplified state"
% (numer, denom))
#Following 'if' block establishes if numerator and/or denominator is 0,
#if numerator and denominator are equal,
#and if numerator or denominator == 1 and/or -1
if denom == 0 and numer == 0:
return "%d/%d = 0" % (numer, denom)
elif denom == 0:
return "Division by 0 - result 'undefined'"
elif numer == 0:
return "0/%d = 0" % denom
elif abs(numer) == abs(denom) and negative == False:
return "%d/%d = 1" % (numer, denom)
elif abs(numer) == abs(denom) and negative == True:
return "%d/%d = -1" % (numer, denom)
elif abs(numer) == 1:
return simplified_statement
elif abs(denom) == 1 and negative == False:
return "%d/%d = %d" % (numer, denom, abs(numer))
elif abs(denom) == 1 and negative == True:
return "%d/%d = -%d" % (numer, denom, abs(numer))
else:
pass
#find all multiples for numerator and denominator, except 1
numer_multiples = []
for i in xrange(2, abs(numer)+1):
if abs(numer) % i == 0:
numer_multiples.append(i)
denom_multiples = []
for i in xrange(2, abs(denom)+1):
if abs(denom) % i == 0:
denom_multiples.append(i)
shared = []
#establish if both numerator and denominator are prime numbers
if len(numer_multiples) == 1 and len(denom_multiples) == 1:
return simplified_statement
#if numerator greater than denominator, find greatest common multiple
#for denominator and divide numerator and denomator by it
elif abs(numer) > abs(denom):
for i in denom_multiples:
if i in numer_multiples:
shared.append(i)
if len(shared) == 0: #no common multiple
return simplified_statement
else:
return "%d/%d is simplified to %d/%d" %(numer, denom,
numer / max(shared), denom / max(shared))
#if denominator greater than numerator, find greatest common multiple
#for numerator and divide numerator and denomator by it
else:
for i in numer_multiples:
if i in denom_multiples:
shared.append(i)
if len(shared) == 0: #no common multiple
return simplified_statement
else:
return "%d/%d is simplified to %d/%d" %(numer, denom,
numer / max(shared), denom / max(shared))
print simplify_fraction(int(raw_input("Type in numerator >")),
int(raw_input("Type in numerator >")))
Fractions
module. docs.python.org/3.6/library/fractions.html \$\endgroup\$