473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

random / lists

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
Jul 18 '05 #1
8 1851
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

Jul 18 '05 #2
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
Jul 18 '05 #3
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

Jul 18 '05 #4
"M. Clift" <no***@here.com> 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?
Jul 18 '05 #5
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
Jul 18 '05 #6
Hi Paul,

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

Malcolm
Jul 18 '05 #7
"M. Clift" <no***@here.com> wrote in message
news:cf**********@newsg2.svr.pol.co.uk...
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.
Jul 18 '05 #8
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
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
7
by: Jean-Francois.Doyon | last post by:
Hello, I'm trying to retrieve a limited number of random rows, and order them by a column, and am not having any luck with that last part: SELECT * FROM tablename ORDER BY random(), id LIMIT...
9
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a...
2
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
4
by: philly_bob | last post by:
In the sample program below, I want to send a random method to a class instance. In other words, I don't know which method to send until run-time. How can I send ch, which is my random choice, to...
6
by: Lanny | last post by:
Well the othe day I was making a program to make a list of all the songs in certian directorys but I got a problem, only one of the directorys was added to the list. Heres my code: import random...
0
by: Edwin.Madari | last post by:
use songs.extend( asongs ) #append is for single item - where ever it mightbe. good luck. Edwin -----Original Message-----
3
by: Manuel Ebert | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Dear list, who's got aesthetic advice for the following problem? I've got some joint probabilities of two distinct events Pr(X=x, Y=y), stored...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.