Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dev/pacmath.activity/questionGenerator.py
blob: 0fe42de2a65cb7c586b1dd494a0cee3189921cc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#! /usr/bin/env python
"""
Creates random questions for PacMath
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
"""
from random import randint
import pygame

class questionGenerator:
        """
        A reusable object which is used to generate questions
        """
	def __init__(self, operation, minOp, maxOp):
        	"""
        	Constructor
                @param operation: The operation that the math questions will incorporate
                @param minOp: The minimum allowable value for an operand
                @param maxOp: The maximum allowable value for an operand
       		"""
		self.minOp = minOp
		self.maxOp = maxOp
		self.operation = operation
	
	def getQuestion(self):
                """
                Returns a single question based on the constraints the generator
                was created with
                @rtype: ("q", "a")
                @return: A single question answer tuple
                """
		op1 = randint(self.minOp, self.maxOp)
		op2 = randint(self.minOp, self.maxOp)	

		if( self.operation == ['x'] ):
			answer = op1*op2
			return (str(op1)+" x " + str(op2) + " = ?", str(answer))
                if( self.operation == ['+'] ):
                        answer = op1+op2
			return (str(op1)+" + "+ str(op2) + " = ?", str(answer))
                elif( self.operation == ['-'] ):
                        answer = op1-op2
			return (str(op1)+" - "+ str(op2) + " = ?", str(answer))
                elif( self.operation == ['/'] ):
                        answer = op1*op2
			return (str(answer)+" / "+ str(op1) + " = ?", str(op2))
		else:
			return null
	def getQuestionSet(self):
                """
                Returns a set of four questions for use in a round of pacmath, 
                ensures that no questions have duplicate answers.
                Fails and exits if four questions with distinct answers cannot
                be generated from this constraint set (eg minOp = 2, maxOp = 3:
                only 2 * 2, 2 * 3, 3 * 3)
                @rtype: [("q", "a"), ("q", "a"), ("q", "a"), ("q", "a")]
                @return: A list of four questions with no repeat answers
                """
		questions = []
		attempts = 0
		while len(questions) < 4 and attempts < 100:
			toAdd = self.getQuestion()
			notDuplicate = True
			for i in questions:
				if toAdd[1] == i[1]:
					notDuplicate = False
					attempts += 1					
			if notDuplicate:
				questions.append( toAdd )
		
		if attempts >= 100:
			print "Bad question set, not enough questions could be generated"
			print "Minimum Operand: " + str(self.maxOp)
			print "Maximum Operand: " + str(self.minOp)
			print "Operation: " + str(self.operation)
			raise SystemExit
		return questions