# Word Search puzzle using a 2d array and modules

Page 1 of 1

## 3 Replies - 8232 Views - Last Post: 14 April 2009 - 02:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=97152&amp;s=88258e5f52cab05995eaeeee8d079250&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jam jam

Reputation: 0
• Posts: 11
• Joined: 01-April 09

# Word Search puzzle using a 2d array and modules

Posted 03 April 2009 - 02:05 PM

Hello,

I am struggling to understand how to create a word search puzzle using modules and 2d arrays. So far for the grid I have the following - what I do not know is how to create a for loop to get rid of the commas between the random letters or how to get Letters at the top of teh column... it should be A-whatever at the top and 1 to what ever down the left hand side.

[import sys
import random
seq='abcdefghijklmnopqrstuvwxyz'
#print random.choice(seq)
def populate_array(rows, columns):
array_dic = {}
for row in range(1, rows+1): ## starts with one, not zero
array_dic[row] = []
for col in range(0, columns):
array_dic[row].append(random.choice(seq))
return array_dic

if __name__ == "__main__":
rows = 10
columns = 10
ret_dic = populate_array(rows, columns)
for key in ret_dic.keys():
print key, ret_dic[key]
]

This post has been edited by jam jam: 03 April 2009 - 02:57 PM

Is This A Good Question/Topic? 0

## Replies To: Word Search puzzle using a 2d array and modules

### #2 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: Word Search puzzle using a 2d array and modules

Posted 03 April 2009 - 04:55 PM

You could start with a matrix like this ... ( a list of Python lists ) ...

```#python 2.6.1
def populate_matrix(rows, cols):
import random
seq = [chr(i) for i in range(ord('a'), ord('a')+cols)]
matrix = []
for row in range(rows):
array = []
for col in range(cols):
array.append(random.choice(seq))
matrix.append(array)
return matrix

if __name__ == "__main__":
rows = 10
columns = 20
matrix = populate_matrix(rows, columns)
print '	 ',
for i in range( ord('A'), ord('A')+columns ): print chr(i),
print
print '	 ',
for i in range(columns): print '-',
print
for i in range(rows):
print '%2d | ' % (i+1),
for col in matrix[i]:
print col,
print

```

This post has been edited by David W: 03 April 2009 - 04:57 PM

### #3 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

## Re: Word Search puzzle using a 2d array and modules

Posted 03 April 2009 - 05:26 PM

Or ... in Python 3.0.1

```#python 3.0.1
def populate_matrix(rows, cols):
import random
seq = [chr(i) for i in range(ord('a'), ord('a')+cols)]
matrix = []
for row in range(rows):
array = []
for col in range(cols):
array.append(random.choice(seq))
matrix.append(array)
return matrix

if __name__ == "__main__":
rows = 10
columns = 20
matrix = populate_matrix(rows, columns)
print('	 ', end=' ')
for i in range( ord('A'), ord('A')+columns ): print(chr(i), end=' ')
print()
print('	 ', end=' ')
for i in range(columns): print('-',end=' ')
print()
for i in range(rows):
#print('%2d | ' % (i+1), end=' ')
print('2d | '.format(i+1), end=' ')
for col in matrix[i]:
print(col,end=' ')
print()

```

Shalom,
David
http://developers-he.../index.p...opic,127.0.html
http://developers-he.../index.p...opic,106.0.html
http://developers-he...index.php/topic,46.0.html

This post has been edited by David W: 03 April 2009 - 06:06 PM

### #4 Stormahawk

Reputation: 0
• Posts: 1
• Joined: 14-April 09

## Re: Word Search puzzle using a 2d array and modules

Posted 14 April 2009 - 02:52 PM

Is there anyway to do this in C#?