2

I have a list of lists of - characters which acts as a grid.

I want to change one - to a Q per col and row.

Here's what I've got so far:

   import pprint 
   import random # I import these classes
   grid = [['-'] for n in range(8)]
   for i in range (8):
       for j in range(8):
           inserPoint = random.randrange(8,8)
           if (j == inserPoint or i == inserPoint) and (grid[j] != 'Q' or grid[i] != 'Q'):
               grid[i][j] = ('Q')
   pprint.pprint(grid) #/ how to print one queen per line 

this is my output. As you can see there are too many Qs on the grid:

[['-','-','-','-','-','-','Q','-'],
 ['-','-','-','-','Q','Q','-','-']
 ['-','-','-','-','Q','-','-','-']
 ['Q','Q','-','-','-','Q','Q','-']
 ['-','-','Q','-','Q','-','-','-'].
smci
  • 32,567
  • 20
  • 113
  • 146
odisho eivazi
  • 65
  • 1
  • 1
  • 8

1 Answers1

3

A few things:

  1. You've got one too many loops. You can loop over the first index i, then just insert the Q where randrange tells you.
  2. Your randrange call is wrong. It should have a start and a stop. In fact, rangerange is not the right tool here if you want your Qs to be unique in row and column.
  3. Your grid set up doesn't actually produce what you show it producing.

Here's all three things fixed:

In [34]: size = 8
In [35]: grid = [['-' for m in range(size)] for n in range(size)]
In [36]: insertPoints = range(size) # = list(range(size)) in Python 3
In [37]: random.shuffle(insertPoints)
In [38]: for i in range(size):
   ....:     grid[i][insertPoints[i]] = "Q"

Which gives:

In [39]: grid
Out[39]: 
[['-', '-', '-', 'Q', '-', '-', '-', '-'],
 ['-', 'Q', '-', '-', '-', '-', '-', '-'],
 ['-', '-', '-', '-', '-', '-', 'Q', '-'],
 ['-', '-', '-', '-', '-', 'Q', '-', '-'],
 ['Q', '-', '-', '-', '-', '-', '-', '-'],
 ['-', '-', '-', '-', 'Q', '-', '-', '-'],
 ['-', '-', '-', '-', '-', '-', '-', 'Q'],
 ['-', '-', 'Q', '-', '-', '-', '-', '-']]

(See this for why shuffling a range doesn't work in Python 3)

Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201
  • we still have Q on top of each other – odisho eivazi Jun 20 '15 at 16:51
  • Oh sorry. Yes, I didn't quite read closely enough. I'll fix it. – LondonRob Jun 20 '15 at 16:54
  • I appreciate your help sir – odisho eivazi Jun 20 '15 at 16:57
  • import random size = 8 grid = [['-' for m in range(size)] for n in range(size)] insertPoints = range(size) random.shuffle(insertPoints) for i in range(size): grid[i][insertPoints[i]] = "Q" Traceback (most recent call last): File "C:\Users\Odisho\Downloads\8queens.py", line 5, in random.shuffle(insertPoints) File "C:\Python34\lib\random.py", line 272, in shuffle x[i], x[j] = x[j], x[i] TypeError: 'range' object does not support item assignment >>> – odisho eivazi Jun 20 '15 at 17:18
  • @LondanRob i got the error message Traceback (most recent call last): File "C:\Users\Odisho\Downloads\8queens.py", line 5, in random.shuffle(insertPoints) File "C:\Python34\lib\random.py", line 272, in shuffle x[i], x[j] = x[j], x[i] TypeError: 'range' object does not support item assignment >>> – odisho eivazi Jun 20 '15 at 17:26
  • 1
    Ah, I think we have a Python 2.7 vs Python 3 problem here. See [this](http://stackoverflow.com/a/20484205/2071807) for your solution. – LondonRob Jun 20 '15 at 17:31
  • @LondonRay i believe problem is """ random.shuffle(insertPoints)""' – odisho eivazi Jun 20 '15 at 17:38
  • 1
    Yes. I've already fixed the question. Use the code in the comment to line `In[36]` and the `shuffle` should work. [Here](https://repl.it/tZf) is the Python 3 code you need. – LondonRob Jun 20 '15 at 17:41
  • @LondanRay, sorry to bother you this much, but it prints none when i run it. – odisho eivazi Jun 20 '15 at 18:41