[Tutor] results not quite 100 percent yet
Tyler Smith
tyler.smith at mail.mcgill.ca
Thu Feb 21 16:39:07 CET 2008
On 2008-02-20, bhaaluu <bhaaluu at gmail.com> wrote:
> As far as I can see, these routines give me the results
> I'm looking for. I get a distribution of four negative numbers,
> four positive integers in the range 10 to 110, and nothing
> is placed in room 6 or room 11:
>
Just for the hell of it, here's my stab at the problem. Note that your
spec doesn't match your code, as there's something special about room
13, which is also excluded from this process. Also, since you appear
to be adding treasure and aliens to the same cell in each list, I've
rolled the procedure up into a single while loop.
The algorithm, such as it is, is just four lines to build a list of
modifiable rooms and a list of additions, and a four line loop that
does the work. As I said, I'm new to Python, so I suspect someone who
knows how to use list comprehensions or generators could clean it up
further. Also, the tmp = random.choice() bit might be replaceable by
some kind of random.pop() thing, but I couldn't figure that out. That
would cut the last line out of the loop.
Not the short is better than clear, but I don't think this is very
complicated, and it takes advantage of Python's listy goodness.
My 2 cents!
Tyler
import random
entrance = 6
exit = 11
death_room = 13
table= [[ 0, 2, 0, 0, 0, 0, 0],
[ 1, 3, 3, 0, 0, 0, 0],
[ 2, 0, 5, 2, 0, 0, 0],
[ 0, 5, 0, 0, 0, 0, 0],
[ 4, 0, 0, 3,15,13, 0],
[ 0, 0, 1, 0, 0, 0, 0],
[ 0, 8, 0, 0, 0, 0, 0],
[ 7,10, 0, 0, 0, 0, 0],
[ 0,19, 0, 0, 0, 8, 0],
[ 8, 0,11, 0, 0, 0, 0],
[ 0, 0,10, 0, 0, 0, 0],
[ 0, 0, 0,13, 0, 0, 0],
[ 0, 0,12, 0, 5, 0, 0],
[ 0,15,17, 0, 0, 0, 0],
[14, 0, 0, 0, 0, 5, 0],
[17, 0,19, 0, 0, 0, 0],
[18,16, 0,14, 0, 0, 0],
[ 0,17, 0, 0, 0, 0, 0],
[ 9, 0, 0,16, 0, 0, 0]]
room_list = range(0,19) ## the list of rooms to modify
## exclude the special rooms:
for i in [entrance, exit, death_room]: room_list.remove(i)
## build a list of random treasure and four aliens:
additions = random.sample(range(10,110), 3)
additions.extend(range(-4,0))
while (additions): ## continue until all additions are added
tmp = random.choice(room_list) ## randomly pick a room
if table[tmp][6] == 0:
table[tmp][6] = additions.pop() ## add a value
room_list.remove(tmp) ## don't pick the same room twice!
## not strictly necessary
for i in table: print i
More information about the Tutor
mailing list