random / lists

wes weston wweston at att.net
Sun Aug 15 23:40:59 EDT 2004


M. Clift wrote:
> Hi Wes,
> 
> Thanks for responding.
> 
> What I want is a system that controls the direction in a generated list. If
> the user selects a number a letter is choosen for each number from at random
> from an array. That's fine and easy to do. The hard part is, is that no
> letter can be repeated with only one letter interveining and the letters can
> only follow each other under certain rules.
> 
>  These being :
> 1. 'a' can be followed by any letter
> 2. 'b' can only be followed by 'c'
> 3. 'c' can be followed by 'a' or 'd'
> 4 'd' can be followed by 'a' or 'c'
> 
> Really it doesn't matter what these rules are for the time being it's just
> working out a way to control the outcome of a list of unknown size. I've
> experimented with lists of say up to 10 long using ' if ' statements for
> each number and different array depending upon the previous letter, but as
> you can imagine it is no good for larger lists as the code just goes on and
> on :  ). Rather than I have done before, whereby I said if letter 3 == 'a'
> then letter 4 = choice(bcd) is it possible to say if previous letter equals
> whatever then this letter equals whatever?
> 
> Wes, if you recieved an email direct to you address I'm sorry. I must
> be tierd, as for some reaon I've been hitting the reply button all night
> instead of the reply to group!
> 
> Thanks
> 
> 

Clift,
    I think in your loop you need to have a variable, call it previousLetter,
to test the latest letter against.

previousLetter = None
for x in range(someNum):
	newLetter = someSelection()
	if not previousLetter or someTestOk( previousLetter, newLetter ):
		doYourStuff()
	previousLetter = newLetter

You might need this in your test:

 >>> ord("c")
99

to test if a character follows and by how much.

wes
wes




More information about the Python-list mailing list