Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #This program give the least commond denominator
- #and equivalent fractions for n number of fractions
- #James Tuvell
- # This is revision 2.0
- # 2-11-12
- """
- * change log
- * adding support for fraction class 2-11-02
- * simplifying functions
- """
- import math
- import fractions
- #========== Begining defs =======================
- def is_number(string):
- try:
- float(string)
- return True
- except ValueError:
- print "This is not a number"
- return False
- def is_fraction(string, pattern):
- beforeslash = ""
- afterslash = ""
- if in_string(string, pattern) != True:
- print "Incorrect format numerator/denominator"
- return False
- if string.count(pattern) > 1:
- print "To many // in fraction."
- return False
- beforeslash = string[:string.find(pattern)]
- afterslash = string[string.find(pattern)+1:]
- if is_number(beforeslash) == False:
- return False
- if is_number(afterslash) == False:
- return False
- if beforeslash.count(".") > 0:
- print "do not put decimal in numerator"
- return False
- if afterslash.count(".") > 0:
- print "do not put decimal in denominator"
- return False
- if int(afterslash) == 0:
- print "Zero in denominator error"
- return False
- if int(beforeslash) == 0:
- print "Did you really want a Zero there?"
- print "Oh well wont hurt anything"
- print "letting error slip"
- return True
- def in_string(string, pattern):
- if string.count(pattern) == 0:
- return False
- return True
- def validator(validation_type = "string", string = "", pattern = ""):
- """Redirects to the validator for each type
- valid types are
- "number"
- "string"
- "fraction"
- """
- if validation_type == "number":
- return is_number(string)
- if validation_type == "string":
- return in_string(string, pattern)
- if validation_type == "fraction":
- return is_fraction(string, pattern)
- print "Invalid validation type :",validation_type
- return False
- def getstring(prompt, maxlength):
- emptystring = ""
- try:
- inputstring = raw_input(prompt)
- if len(inputstring) > maxlength:
- print "Too long max size is ",maxlength," characters"
- return emptystring
- return inputstring
- except ValueError:
- print "Invalid input!"
- return emptystring
- except:
- print "This is a unexpected : ",Exception
- print "Thowing out exception"
- return emptystring
- return
- def getfractionlist(prompt, terminator):
- doneflag = False
- maxlength = 30
- string = ""
- fractionlist = []
- cfraction = fractions.Fraction(1,1)
- while doneflag == False :
- string = ""
- string = getstring(prompt, maxlength)
- if string == terminator:
- return fractionlist
- else:
- validation_type = "fraction"
- pattern = "/"
- if validator(validation_type, string, pattern) == True:
- cfraction = fractions.Fraction(string)
- fractionlist.append(cfraction)
- else:
- doneflag == False
- return fractionlist
- def dupli(the_list):
- donelist=[]
- for item in sorted(set(the_list)):
- donelist = donelist + [(item, the_list.count(item)),]
- return donelist
- #
- # name: primetest
- # @param positive integer
- # @return boolean true or false
- # Test to see if the number is prime
- def primetest(num):
- """Test primeness return boolean True or False"""
- prime=True
- if num == 1:
- prime=False
- return prime
- i = 2
- while i <= math.sqrt(num):
- if num % i == 0:
- prime = False
- break
- i=i+1
- return prime
- #
- # name: nextPrime
- # @param positive integer prime number
- # @return next positive prime integer
- # Find the next prime up from the one given
- def nextPrime(num):
- """Find next prime from current number"""
- nextone = num + 1
- while primetest(nextone) == False:
- nextone = nextone + 1
- return nextone
- #
- # name: iter_prime
- # @param positive integer
- # @return list of prime integers
- # Create a list of prime factors for the number given
- def iter_prime_factors(intnumber):
- """Make a list of prime factors from number [2,2,2,3,3,5]"""
- factor_list = []
- factored_number = intnumber
- if primetest(intnumber) != False:
- factor_list.append(intnumber)
- else:
- primefactor = 2
- while factored_number != 1:
- while factored_number % primefactor == 0:
- factored_number = factored_number / primefactor
- factor_list.append(primefactor)
- primefactor = nextPrime(primefactor)
- return factor_list
- def factor_denominator(denominator_list):
- lcd = 1
- prime_factor_list = []
- combinedlist = []
- for i in range(len(denominator_list)):
- prime_factor_list.append(iter_prime_factors(denominator_list[i]))
- for i in range(len(prime_factor_list)):
- for item in prime_factor_list[i]:
- if combinedlist.count(item) < prime_factor_list[i].count(item):
- combinedlist.append(item)
- for item in combinedlist:
- lcd = lcd * item
- return lcd
- def lcd(fractionlist):
- lcd = 0
- denomlist =[]
- denominator = 1
- for item in fractionlist:
- denominator = item.denominator
- denomlist.append(denominator)
- lcd = factor_denominator(denomlist)
- return lcd
- def print_equivalent_Fractions(fractionlist, lcd):
- formatter = " %d/%d ----------> %d/%d"
- print
- print "The least common denominator for this group is :",lcd
- print
- for i in range(0,len(fractionlist)):
- numerator = fractionlist[i].numerator
- denominator = fractionlist[i].denominator
- print formatter % (numerator,denominator,numerator*(lcd/denominator),lcd)
- #========== End of defs =========================
- prompt = "Enter a fraction or q to quit :"
- terminator = "q"
- myfraction = fractions.Fraction()
- fractionlist =[]
- fractionlist = getfractionlist(prompt,terminator)
- lcd = lcd(fractionlist)
- print_equivalent_Fractions(fractionlist, lcd)
- print "Thank you!"
Advertisement
Add Comment
Please, Sign In to add comment