3 Replies - 6989 Views - Last Post: 14 April 2009 - 02:52 PM Rate Topic: -----

#1 jam jam  Icon User is offline

  • New D.I.C Head

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

Word Search puzzle using a 2d array and modules

Post icon  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  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 Stormahawk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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#?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1