Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
27 lines (23 loc) · 749 Bytes

File metadata and controls

27 lines (23 loc) · 749 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#/usr/bin/env python
import random
def queens(num=8, state=()):
for pos in range (num):
if not conflict(state, pos):
if len(state) == num-1:
yield (pos,)
else:
for result in queens(num, state + (pos,)):
yield (pos, ) + result
def conflict(state, nextX):
nextY = len(state)
for i in range (nextY):
if abs (state[i] - nextX ) in (0, nextY - i):
return True
return False
def prettyprint(solution):
def line(pos, length = len(solution)):
return '. ' * (pos) + 'X ' + '. ' * (length - pos - 1)
for pos in solution:
print line(pos)
if __name__ == '__main__':
prettyprint(random.choice(list(queens(8))))
Morty Proxy This is a proxified and sanitized view of the page, visit original site.