random / lists

M

Malcolm Clift

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
 
W

wes weston

Malcolm said:
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
 
M

M. Clift

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
 
W

wes weston

M. Clift said:
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:
99

to test if a character follows and by how much.

wes
wes
 
P

Paul Rubin

M. Clift said:
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?
 
M

M. Clift

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

M. Clift

Hi Paul,

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

Malcolm
 
R

Russell Blau

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

M. Clift

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top