On Wed, Feb 3, 2010 at 10:06 AM, David <ld...@gmx.net> wrote: > Bob, > > brilliant stuff -- I am truly awed by this. Create a default-filled matrix > and mark combinations used so as to take them out of the game? Wow. This is > new to me. > > On 03/02/10 15:46, bob gailer wrote > >> def askQuestions(): # generate and ask questions: >> for i in range(NQ): >> while 1: # loop till we get an unused combo >> x, y = [random.randint(1,MAX) for i in 'ab'] >> if mtable[x][y] == 1: # combo is available >> break >> askQuestion(x,y) >> # indicate asked >> mtable[x][y] = 0 >> mtable[y][x] = 0 > > Here you lose me, though. Where does mtable come from, what does it do? You > don't introduce it as a variable, and when I google it, nothing much comes > of if... >
I think mtable should be replaced by the pool variable defined at the top. Then it makes sense. Second, I think a two-dimensional approach is rather wasteful here, it's clearer and simpler to keep a list of generated questions (you already do that anyway), and for each new question answered, do: if (x, y) in generated: continue else: generated.append((x, y)) The random generating approach is more efficient if you're only going to use a small subset of the possible set of questions. If you're going to use almost the entire set, though, calculating them is more efficient, since the random generator will come across more and more duplicates as it fills up the array. Here's an alternative approach that uses itertools.combinations to calculate all possible sequences and then shuffles: import itertools import random # maximum number and number of questions MAX = 20 NQ = 10 def gen_questions(): # start at 2, since 0 * x and 1 * x aren't very interesting q = list(itertools.combinations(range(2, MAX), 2)) random.shuffle(q) return q Note that, if MAX is high, it will take a very long time to generate all possible questions. (1000 was a second or so on my computer, but 10.000 took longer than I was willing to wait). If you're not going to use all of those questions, (and it's likely you won't, since a range of ten numbers already provides 45 different questions), then the random generation is better. Hugo _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor