Hi All,

The recent discussion regarding fractions compelled me to dig-up a
fraction problem generating script I wrote a couple years ago and post
it in case anyone has use for it.  The purpose of the script is to
generate algebraic fraction problems.   It might also make a decent
programming assignment to come up with such an algorithm.  Just run it
and you'll get 100 good problems dumped to your screen.  Change the 2
parameters in the __main__ to adjust number generated and maximum
denominators to use.   I've never looked at Fraction.py, so no idea
how this compares or relates, but don't think there's any similarity.
Anyway, FWIW here it is (attached).

All the best to everyone,
Charlie

-- 
AsymptopiaSoftware|softw...@thelimit
            http://www.asymptopia.org
#!/usr/bin/env python
from random import random

"""
steps involved in generating arbitrary fraction equations
of the form: a/d <+,-,*> b/e=c/f

1. generate a,b,d,e; make sure denoms != 0
2. find LCD
3. pose problem (i.e. choose an operation)
4. solve the problem (i.e. determine c,f)
5. reduce "c/f"
6. append to collection of generated equations...?
"""

debug=0
class fracmaker:
	def __init__(self,maxnum,maxden):
		if(debug):print 'fracmaker.__init__'
		self.maxnum=maxnum
		self.maxden=maxden
		self.LCD=None
		self.GCF=None
		self.problems=[]
		self.op=''
		self.a=0
		self.b=0
		self.c=0
		self.d=0
		self.e=0
		self.f=0
		
	
	def make1(self):
		if(debug):print 'make1()'
		self.generate()
		self.findLCD()
		self.poseProblem()
		self.solveProblem()
		self.reduce()
		self.convert2algebra()
		
	def generate(self):
		maxnum=self.maxnum
		maxden=self.maxden
		self.a=int(maxnum*random())
		self.b=int(maxnum*random())
		self.d=0
		self.e=0
		while self.d==0:
			self.d=int(maxden*random())
		while self.e==0:
			self.e=int(maxden*random())
		if(debug):print "a=%d b=%d d=%d e=%d"%(self.a,self.b,self.d,self.e)
		
	def findLCD(self):
		self.LCD=self.d*self.e
		if(debug):print 'LCD=',self.LCD

	def poseProblem(self):
		ops=['+','-','*']
		idx=int(len(ops)*random())
		self.op=ops[idx]
		if(debug):print self.op

	def solveProblem(self):
		if(debug):print "solving: %d/%d %s %d/%d = c/f"%(self.a,self.d,self.op,self.b,self.e)
		if self.op=='+':
			self.c=self.a*self.e+self.b*self.d
			self.f=self.LCD
		elif self.op=='-':
			self.c=self.a*self.e-self.b*self.d
			self.f=self.LCD
		elif self.op=='*':
			self.c=self.a*self.b
			self.f=self.d*self.e
		
		if(debug):print "solution: %d/%d %s %d/%d = %d/%d"%(self.a,self.d,self.op,self.b,self.e,self.c,self.f)
		
	def reduce(self):
		if self.c==0:return
		
		if self.c<self.f:startval=self.c
		else:startval=self.f
		if startval<0:startval=startval*-1
		for gcf in range(startval,0,-1):
			if int(self.c/gcf)==float(self.c)/float(gcf) and int(self.f/gcf)==float(self.f)/float(gcf):
				self.GCF=gcf
				break
		
		self.c=self.c/self.GCF
		self.f=self.f/self.GCF
		if(debug):print "reduced solution: %d/%d %s %d/%d = %d/%d"%(self.a,self.d,self.op,self.b,self.e,self.c,self.f)
	
	def convert2algebra(self):
		idx=int(random()*6)	
		if idx==0:
			xval=self.a
			problem="%d:x/%d %s %d/%d = %d/%d"%(xval,self.d,self.op,self.b,self.e,self.c,self.f)
		if idx==1:
			xval=self.b
			problem="%d:%d/%d %s x/%d = %d/%d"%(xval,self.a,self.d,self.op,self.e,self.c,self.f)
		if idx==2:
			xval=self.c
			problem="%d:%d/%d %s %d/%d = x/%d"%(xval,self.a,self.d,self.op,self.b,self.e,self.f)
		if idx==3:
			xval=self.d
			problem="%d:%d/x %s %d/%d = %d/%d"%(xval,self.a,self.op,self.b,self.e,self.c,self.f)
		if idx==4:
			xval=self.e
			problem="%d:%d/%d %s %d/x = %d/%d"%(xval,self.a,self.d,self.op,self.b,self.c,self.f)
		if idx==5:
			xval=self.f
			problem="%d:%d/%d %s %d/%d = %d/x"%(xval,self.a,self.d,self.op,self.b,self.e,self.c)
		if(debug):print problem
		self.problems.append(problem)
	
	def dump(self):
		for idx in range(len(self.problems)):
			print self.problems[idx]
		
if __name__=='__main__':
	x=fracmaker(10,10)
	for idx in range(100):
		x.make1()
	x.dump()

_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to