# random / lists

Discussion in 'Python' started by Malcolm Clift, Aug 14, 2004.

1. ### Malcolm CliftGuest

Hi All,

I have this so far.

import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

l = []

for x in things:
tmp = random.choice('abcd')
print state
while tmp in state[0:2]:
tmp = random.choice('abcd')
print "choice ",x+1," is ", tmp
l.append(tmp)

state[x%2] = tmp

print 1

Could someone help in showing me how to control it like this? That if 'a'
is choosen it can only be followed by say b or d, if 'd' is choosen it can
only be followed by c or b etc... I can see how to do this after the list
has been generated with if statements, but that is impractical. As it is
at the moment it stop two of the same letters being given one after
another or the same letter occurring with only one space interviening.

Thanks,

Calvin

Malcolm Clift, Aug 14, 2004

2. ### wes westonGuest

Malcolm Clift wrote:
> Hi All,
>
> I have this so far.
>
>
> import random
>
> things = xrange(int(raw_input("choose no of things (1-8)? ")))
>
> state = [None,None]
>
> l = []
>
> for x in things:
> tmp = random.choice('abcd')
> print state
> while tmp in state[0:2]:
> tmp = random.choice('abcd')
> print "choice ",x+1," is ", tmp
> l.append(tmp)
>
> state[x%2] = tmp
>
> print 1
>
> Could someone help in showing me how to control it like this? That if 'a'
> is choosen it can only be followed by say b or d, if 'd' is choosen it can
> only be followed by c or b etc... I can see how to do this after the list
> has been generated with if statements, but that is impractical. As it is
> at the moment it stop two of the same letters being given one after
> another or the same letter occurring with only one space interviening.
>
> Thanks,
>
> Calvin
>
>

Calvin,
Could you give a clear statement of what you want to
do. No code; just a description of what you want to do.
wes

wes weston, Aug 15, 2004

3. ### M. CliftGuest

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

Thanks

M. Clift, Aug 16, 2004
4. ### wes westonGuest

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
>
> Thanks
>
>

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

previousLetter = None
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

wes weston, Aug 16, 2004
5. ### Paul RubinGuest

"M. Clift" <> writes:
> 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.

Is this a homework assignment?

Paul Rubin, Aug 16, 2004
6. ### M. CliftGuest

Hi Wes,

Thankyou. I'll now go away and experiment with how to use it as I've just
started with python, but I should be able to learn a lot with this. It would
seem to be exactly what I was after. : )

Malcolm

M. Clift, Aug 16, 2004
7. ### M. CliftGuest

Hi Paul,

No, not homework, I'm a bit old for that : ) It's an idea for a simple
decision maker.

Malcolm

M. Clift, Aug 16, 2004
8. ### Russell BlauGuest

"M. Clift" <> wrote in message
news:cformj\$vso\$...
> 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.

How about if you define a dictionary mapping each letter to a list of the
letters that are allowed to follow it, then use random.choice() to pick one
member of the list. For example,

mapping = {'a': ['b', 'c', 'd'], 'b': ['c'], 'c': ['a', 'd'],
'd': ['a', 'c']}

then after you added the last letter to l, you could just do

allowed = mapping[l[-1]][:] # nneds to be a copy because it may get
modified
if l[-2] in allowed: # no letter can appear two out of three
places
allowed.remove[l[-2]]
l.append(random.choice(allowed)

Of course, this doesn't account for every case (like, it won't work if l is
the empty list), and it may not satisfy all your desired rules, but it's a
start.

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.

Russell Blau, Aug 16, 2004
9. ### M. CliftGuest

Hi Russell,

That looks really good. It would seem to allow for a lot of control. Yet
another example for me to learn from! As someone new to programming, I'm
only just starting to realise that in code there is no one answer to a
problem.

Thanks,

Malcolm

M. Clift, Aug 16, 2004