-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem38.py
More file actions
51 lines (37 loc) · 1.4 KB
/
Copy pathproblem38.py
File metadata and controls
51 lines (37 loc) · 1.4 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# This problem was asked by Microsoft.
# You have an N by N board. Write a function that,
# given N, returns the number of possible arrangements
# of the board where N queens can be placed on the board
# without threatening each other, i.e. no two queens share
# the same row, column, or diagonal.
#Saw it from
#https://github.com/vineetjohn/daily-coding-problem/blob/master/solutions/problem_038.py
def is_safe_position(board, row):
#checking if there is a queen on same line here
if row in board:
return False
col = len(board)
#checking diagonals here
for occupied_col, occupied_row in enumerate(board):
if abs(occupied_row-row) == abs(occupied_col - col):
return False
return True
def queen_configurations(board, n):
if n == len(board):
return 1
count = 0
for row in range(n):
if is_safe_position(board, row):
count += queen_configurations(board + [row], n)
return count
if __name__ == "__main__":
assert not is_safe_position([0, 2], 0)
assert not is_safe_position([0, 2], 2)
assert is_safe_position([0, 8], 3)
assert not is_safe_position([1, 3], 2)
assert is_safe_position([], 1)
assert queen_configurations([], 2) == 0
assert queen_configurations([], 4) == 2
assert queen_configurations([], 5) == 10
assert queen_configurations([], 8) == 92
print('All tests pass!')