This is my code for a sudoku solver and I am getting this error:
File "c:/Users/Documents/untitled-17.py", line 85, in <module>
if board[i][j] == 0:
builtins.IndexError: string index out of range
My code is below and I am unsure of the error and how to fix it. I am not sure if the error is because of the main function or the solveBoard function. I am also unsure if this is the only error I will run into after I fix this one error. I am afraid that if I fix this one error I might screw up my whole program. This is the import file I am reading from, only difference is there is no extra new line in between the numbers. Here is the file:
9 0 5 0 0 0 0 0 0
0 6 3 1 0 0 0 0 5
4 0 0 6 5 0 0 7 2
0 7 0 9 6 0 0 2 3
2 0 9 0 0 0 1 0 6
3 4 0 0 1 5 0 8 0
1 9 0 0 2 7 0 0 8
6 0 0 0 0 1 2 4 0
0 0 0 0 0 0 7 0 1
def main():
L = []
fileName = input('Enter input file name: ')
inFile = open(fileName,'r')
print('Initial Game')
line = 9
for x in range(line):
a = inFile.readline()
first = a.split()
H = first
H = (str(H).replace("'",""))
L.append(H)
x = (str(L).replace("'",""))
y = x.replace("],", "],\n")
return y
myBoard = main()
def isValid(board, row, col, num):
#check row
for i in range(9):
if board[row][i] == num:
return False
#check col
for i in range(9):
if board[i][col] == num:
return False
#get top-left corner
c_row = row - row%3
c_col = col - col%3
#check 3x3 square
for i in range(c_row, c_row+3):
for j in range(c_col, c_col+3):
if board[i][j] == num:
return False
#return True if none of the cases above returns False
return True
def solveBoard(board):
for i in range(9):
for j in range(9):
if board[i][j] == 0:
for num in range(1,10):
if isValid(board, i, j, num):
board[i][j] = num
result = solveBoard(board)
if result == True:
return True
else:
board[i][j] = 0
return False
return True
solveBoard(myBoard)
for line in myBoard:
print(line)
#check col
for i in range(9):
should be
for j in range(9):
Running this code, it looks like the board array being passed to solveBoard() is 269 x 1 and you're treating it like it's 9 x 9
I can't check without the full input file, but I think the following is likely to fix your file input parsing:
def parse_board_file(filepath):
board = []
with open(filepath, 'r') as input_file:
print('Initial Game')
row = []
for line in input_file:
for character in line:
if character.isdigit():
row.append(str(character))
if len(row) == 9:
board.append(row)
row = []
if len(board) == 9:
return board
# if you make it to here, you reached the end of the file before
# finding enough numbers
print("ERROR: a full 9x9 grid could not be parsed from the given file.")
return board
def isValid(board, row, col, num):
# ...rest of function...
def solveBoard(board):
# ...rest of function...
def main():
filepath = input('Enter input file name: ')
board = parse_board_file(filepath)
solveBoard(board)
for line in board:
print(line)
if __name__ == "__main__":
main()
However, with the given test case this still did not succeed in completing the board. Sudoku requires iteratively filling in the next cell that has been narrowed down, so it would be very very unusual for only one pass over the board from top to bottom to be able to solve each square. But even wrapping your code in an extra loop to run until the board is solved likely won't help here.
The following datapoints may help you refine your approach:
The condition to fill in a cell in your code is just that there is no reason that number cannot go there. However, when just starting out more cells will be blank than not, so lots of cells will be able to be filled with this logic. Once some have been filled in, you may find there are no valid moves remaining so some of the earlier moves may have been in error. This is because the condition you are looking for in Sudoku is not "can this number go here?" but in fact "does this number have to go here?" and "is this the only number that can go here?" The cell should remain blank until there is only one option that fits it.
Harder sudoku do not rely only on knowing which numbers are already present in a row, column or square. They may rely on conditions such as if the places a certain number can go has been refined to one of a few options that are all in a certain place. e.g.
1 2 3 7 8
4 5 6 7 8 9 1 2
7 8 9 1 2 3 4 5
Square A Square B Square C
In this scenario, there are 3 cells remaining in Square C and if you only considered rows and squares then a 6 could go in either the top or bottom of those cells. However, even though the 6 has not been placed in Square B, we can still know that it will have to be placed in the top row. Therefore, the 6 would need to go in the bottom empty cell in Square C.
I hope this sets you on your way to a solution :)
Related
so I have a data file and I'm wanting to find two things:
Whether the coordinates I give are inside or outside an area and returning if its true or not
Put each coordinates of "1" together in each line in its own list. This should return it in a dictionary.
The file has the following:
1 1 0 1 0 1
0 0 1 1 1 0
1 1 1 1 1 0
1 0 0 0 0 1
0 0 1 1 0 1
1 0 0 0 0 1
I've put the above into a list and each with the code:
lines = []
with open('area.dat', 'r') as a:
for line in a:
line = line.strip()
if not line:
continue
lines.append(list(map(int, line.split())))
data.extend(map(int, line.split()))
print(lines)
My attempts at code to get the coordinates and whether it's outside or inside the area (for both x and y)
area is the list of lists
x = int(input('enter x: '))
y = int(input('enter y: '))
def cords(x, y, area):
if x > 6 or y > 6:
print('Outside Area')
return(False)
else:
print('Inside Area')
return(True)
I want to get a coordinate x and y within the list "area" and return whether it is in or out this.
So for example if I put in cords(0,4,area) it will return "True" and if I put cords(3,7,area) it will return "False".
After this I then want to put them together in groups of 1's by each line.
so for example line 1 and 2 would give:
{1: [(0,4), (0,5)], 2: [(1,0), (1,1)]}
All help is appreciated thanks.
For the first part you have two options:
def cords(x, y):
return x >= 0 and x < 6 and y >= 0 and y < 6
This first option is static for an area size of 6x6, note that array indexing starts at 0 so 6 would already be out of bounds.
def cords(x, y, area):
return x >= 0 and x < len(area) and y >= 0 and y < len(area[0])
This second option dynamically checks whether the coordinates are in bounds of the given nested list. You might have to adjust it based on whether x and y relate to rows and columns or vice versa.
Now for your second part, you're creating a dictionary with redundant information, since the index (1 and 2 in your example) directly relates to the first axis (0 and 1 in your example), you might want to reconsider what you actually want to achieve here.
d = {}
for i,row in enumerate(lines):
n = []
for j,v in enumerate(row):
if v == 1:
n.append([i,j])
d[i+1] = n
Essentially my code looks like this
for i in range(10):
print(i)
if i == 8:
i -=1
That code prints 0 through 10 as normal instead of printing 8 twice. Is there any way to get around that?
Use this custom generator:
def same_number_iterator(last, same_number_twice):
i = 0
while i < last:
yield i
if i in same_number_twice:
yield i
i += 1
for i in same_number_iterator(10, same_number_twice=[8]):
print(i)
Output:
0
1
2
3
4
5
6
7
8
8
9
The solution by Aviv works for the problem you described. However, since the title says "Is it possible to iterate over the same number twice?" maybe what you want to know is how to repeat a certain iteration given a condition. If that's the case, you could simply do this:
repeated = False
i = 0
while i<10:
print(i)
if i == 8 and not repeated:
repeated = True
else:
i += 1
A simple thing that you can do is have an inner for loop, that does either 1 or 2 iterations depending on the value of i.
for i in range(10):
for _ in range(2 if i == 8 else 1):
print(i)
I am trying to produce a code that verifies whether or not a user input meets the criteria of a pascal triangle. I know how to go about inputting the number of lines and having it develop a pascal triangle, but I am having trouble figuring out how to get a user to input something like 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1, and having my program say whether it is a pascal triangle or not.
values = input("Enter the numbers: ").split()
pascals_triangle = list(map(list, values))
I know the first line could split the numbers, and the second line would assign the numbers into individual lists of a list. Every time I attempt to have the lists increase by 1 in every row, I get str/int errors. Once I get past this little road block, I should be able to figure out the rest of the code.
data = input("Enter values: ").split()
def pascal_triangle(data):
size = int(data[0])
n = 2 * size + 1
grid = [[0 for x in range(n)] for y in range(size)]
left = 1
for i in range(size, 0, -1):
grids = data[i].split(' ')
count = 0
for g in grids:
grid[i - 1][left + 2 * count] = int(g)
count += 1
left += 1
if count != i:
return False
left = 1
for i in range(size - 1, -1, -1):
if i == 0:
return grid[i][left] == 1
numbers = i + 1
count = 0
while count < numbers:
current = grid[i][left + count * 2]
upper_left = grid[i - 1][left - 1 + count * 2]
upper_right = grid[i - 1][left + 1 + count * 2]
if current != (upper_left + upper_right):
return False
count += 1
left += 1
return False
status = pascal_triangle(data)
if status:
print('It is a pascal triangle')
else:
print('It is not a pascal triangle')
So, in this code, why am I still not getting the accurate answers?
If you're trying to do this in some fancy way, like adapting the grouper recipe in the itertools docs to take an iterable of group sizes instead of a fixed group size… take a step back and write the "dumb" version first.—just write a loop.
First, split the whole string, the same way you split each line in your line-by-line version.
One thing: mapping list over your values won't do any good; that'll just turn, e.g., '23' into ['2', '3'], and there's not much good you can do with that. You want a list of numbers, which you're then going to break up into a rows (each row also being a list of numbers—the same row you got by mapping int over line.split() in your line-by-line version).
So, here's some pseudocode:
values = input("Enter the numbers: ").split()
nums = [int(value) for value in values]
size = 1
start = 0
while start < len(nums):
rownums = nums[start:start+size]
make sure len(rownums) == size
check rownums the same way you checked each line
update size and start
if you got here without seeing any errors, it's valid
One way to do this is to generate each row of Pascal's triangle, and use islice to grab a list of the current row length from the user data and see if the data matches the row.
from itertools import islice
def pascal():
""" Pascal's triangle generator """
a = [1]
while True:
yield a
#Generate next row from current row
a = [x + y for x, y in zip([0] + a, a + [0])]
def test_pascal(values):
it = map(int, values.split())
ok = True
for row in pascal():
data = list(islice(it, len(row)))
if not data:
break
if data != row:
ok = False
print('bad data', data, row)
break
return ok
# Test
values = '1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1'
print(test_pascal(values))
values = '1 1 1 1 2 1 1 3 3 1 1 4 6 5 1'
print(test_pascal(values))
output
True
bad data [1, 4, 6, 5, 1] [1, 4, 6, 4, 1]
False
I'm coding a program that reads a line in a file and determines whether or not the line makes a Lo Shu Magic square. In this magic square, the sum of the rows, sum of the columns, and sum of the diagonals have to equal 15, and each number 1-9 can only occur once in the square. This is what I have so far:
def main():
for line in open("Magic Square Input.txt"):
items = line.split(" ")
items = [int(x) for x in items]
result = [items[0:3], items[3:6], items[6:9]]
isMagic(result)
def isMagic(result):
checks1 = ''
for x in result:
for y in range(3):
if sum (result[y][y] for y in range(3)) == 15:
if sum(x[y] for x in result) == 15:
checks1 = checkDupe(result)
else:
checks1 = 'Invalid'
else:
checks1 = 'Invalid'
print(checks1)
def checkDupe(result):
checks1 = ''
for i in range(0,8):
counter = 0
for j in result:
if (j == i):
counter += 1
if counter > 0:
checks1 = 'Invalid'
else:
checks1 = 'Valid'
return checks1
main()
the contents of my text file are as follows:
4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5
The first three numbers in each line represent the top row of the square, the next three are the middle row, and the last three are the bottom row. The problem im having is that the first three squares ARE valid, and the last four are supposed to be invalid. But what my code keeps printing out for me is
Valid
Valid
Valid
Valid
Valid
Invalid
Valid
Could somebody show me where I'm screwing up here? I'm fairly new to python and I've been staring at this for hours trying to make sense of it.
This problem is much easier to think about if you start with a flat list:
[4, 3, 8, 9, 5, 1, 2, 7, 6]
and then work out which indexes you need to check. There are only eight in all:
indexes = (
(0, 1, 2), (3, 4, 5), (6, 7, 8), # rows
(0, 3, 6), (1, 4, 7), (2, 5, 8), # cols
(0, 4, 8), (2, 4, 6), # diag
)
With that set up, the check function becomes very simple:
def main():
for line in open('Magic Square Input.txt'):
square = [int(n) for n in line.split()]
if len(set(square)) != len(square):
print('Invalid: Duplicates')
else:
for idx in indexes:
if sum(square[i] for i in idx) != 15:
print('Invalid: Sum')
break
else:
print('Valid')
My version without spliting items into rows
data = '''4 3 8 9 5 1 2 7 6
8 3 4 1 5 9 6 7 2
6 1 8 7 5 3 2 9 4
6 9 8 7 5 3 2 1 4
6 1 8 7 5 3 2 1 4
6 1 3 2 9 4 8 7 5
5 5 5 5 5 5 5 5 5'''
def main():
for line in data.split("\n"):
# create list with all numbers
items = list(map(int, line.split()))
print(is_magic(items))
def is_magic(items):
# --- dups ---
#print('dups')
#print(len(set(items)) == 9)
#for x in range(1, 10):
# print(x, x in items)
if len(set(items)) != 9:
return 'Invalid'
# --- rows ---
#print('rows')
for x in range(0, 9, 3):
l = items[x:x+3]
#print(l, sum(l) == 15)
if sum(l) != 15:
return 'Invalid'
# --- cols ---
#print('cols')
for x in range(3):
l = [items[x], items[x+3], items[x+6]]
#print(l, sum(l) == 15)
if sum(l) != 15:
return 'Invalid'
# --- diags ---
#print('diags')
l = [items[0], items[4], items[8]]
#print(l, sum(l) == 15)
if sum(l) != 15:
return 'Invalid'
l = [items[2], items[4], items[6]]
#print(l, sum(l) == 15)
if sum(l) != 15:
return 'Invalid'
# --- OK ---
return 'Valid'
main()
def magic_square(n):
num=(n*((n*n)+1))/2
print('\nThe Magic Number Is:-',num,'\n')
f=[]
for i in range(0,n):
a=[]
for j in range(0,n):
a.append(0)
f.append(a)
(x,i,p,q)=(n*n,1,int(n/2),n-1)
while x!=0:
if x==0:
(f[p][q],i,p,q,x)=(i,i+1,p-1,q+1,x-1)
continue
else:
if p==-1 and q==n:
p=0
q=n-2
if f[p][q]==0:
(f[p][q],i,p,q,x)=(i,i+1,p-1,q+1,x-1)
continue
else:
p=p+1
q=q-2
f[p][q]=i
i=i+1
p=p-1
q=q+1
x=x-1
continue
if p==-1:
p=n-1
if f[p][q]==0:
(f[p][q],i,p,q,x)=(i,i+1,p-1,q+1,x-1)
continue
else:
p=p+1
q=q-2
f[p][q]=i
i=i+1
p=p-1
q=q+1
x=x-1
continue
if q==n:
q=0
if f[p][q]==0:
(f[p][q],i,p,q,x)=(i,i+1,p-1,q+1,x-1)
continue
else:
p=p+1
q=q-2
f[p][q]=i
i=i+1
p=p-1
q=q+1
x=x-1
continue
else:
if f[p][q]==0:
(f[p][q],i,p,q,x)=(i,i+1,p-1,q+1,x-1)
continue
else:
p=p+1
q=q-2
f[p][q]=i
i=i+1
p=p-1
q=q+1
x=x-1
continue
for i in range(len(f)):
for j in range(len(f[i])):
print(f[i][j] ,end = " ")
print("\n")
INPUT
magic_square(5)
OUTPUT
The Magic Number Is:- 65.0
9 3 22 16 15
2 21 20 14 8
25 19 13 7 1
18 12 6 5 24
11 10 4 23 17
In order to help you, I should start saying that your code is very difficult to read. Since you are new to Python, soon you will find out that one of the major benefits of Python is its clear syntax, which makes very easy to figure out what a piece of code is doing. That being said, I solve your problem, using the same logic as you did, but making the code more readable and using some of Python tricks to make the solution shorter and cleaner.
def main():
"""Open the file, parse the input and check if it is a magic cube"""
with open("Magic Square Input.txt") as f:
for line in f.readlines():
numbers = line.split(" ")
cube = [int(x) for x in numbers]
is_magic(cube)
def is_magic(cube):
"""Check if cube is magic.
There are two conditions that must be satisfied:
1 - There must not be any repetitions of the numbers
2 - All vertical/horizontal/diagonal sums must be 15
"""
if not dupe(cube) and check_sum(cube):
print ('Valid')
else:
print ('Invalid')
def dupe(cube):
"""Check if there are repetitions in the cube."""
if len(cube) == len(set(cube)):
return False
return True
def check_sum(cube):
"""Check if all vertical/horizontal/diagonal sums are 15"""
if vertical_check(cube) and horizontal_check(cube) and diagonal_check(cube):
return True
def vertical_check(cube):
if sum(cube[0:9:3]) == sum(cube[1:9:3]) == sum(cube[2:9:3]) == 15:
return True
return False
def horizontal_check(cube):
if sum(cube[0:3]) == sum(cube[3:6]) == sum(cube[6:9]) == 15:
return True
return False
def diagonal_check(cube):
if sum(cube[0:9:4]) == sum(cube[2:7:2]) == 15:
return True
return False
main()
I hope you can understand the solution from the comments in the code. If this is not the case, please post here again.
I had to make some major changes, but it seemed like your checkDupe method wasn't working right. You also only checked for one diagonal instead of both. Also, note that instead of saving whether the answer is valid or not using a checks1 variable, it simply returns 'Invalid' if anything is wrong, this generally makes code much cleaner and simplified the problem quite a bit. If 'Invalid' is never returned, then the method just returns 'Valid' at the end.
def main():
for line in open("Magic Square Input.txt"):
items = line.split(" ")
items = [int(x) for x in items]
result = [items[0:3], items[3:6], items[6:9]]
print isMagic(result)
def isMagic(result):
# check duplicates
if(checkDupe(result) == 'Invalid'):
return 'Invalid'
# diagonals
if sum (result[y][y] for y in range(3)) != 15:
return 'Invalid'
# other digonals
if sum (result[2 - y][2 - y] for y in range(3)) != 15:
return 'Invalid'
# rows and columns
for i in range(3):
if sum(result[i][y] for y in range(3)) != 15:
return 'Invalid'
if sum(result[x][i] for x in range(3)) != 15:
return 'Invalid'
return 'Valid'
def checkDupe(result):
for x in range(1,9):
if(not x in (result[0]+result[1]+result[2])):
return 'Invalid'
return 'Valid'
main()
Here I have created sample method to solve this issue. In this method, we are maintaining following lists
lstAvailableNumbers: list of available numbers (in your case it would be 1-9)
lstTraversedNumbers: list of traversed numbers (once a item is traversed, it is moved to this list)
list of duplicate numbers (all the duplicate numbers will be added to this list)
duplicate number index (it will store the index of the duplicate number so that it can used to replace the non-existing number
It also return the cost of replacement
For debugging purpose, I have also added print statement to check the value of different list
def createMagicSquare(s):
lstAvailableNumbers = list()
lstAvailableNumbers=[1,2,3,4,5,6,7,8,9]
lstTraversedNumbers=set()
lstDuplicateNumbers=list()
dictDuplicateNumberIndex = dict()
lstMissingNumbers=set()
cost=0
for item in range(len(s)):
for colItem in range(len(s[item])):
num= s[item][colItem]
#print('Print traversed number - ' )
#print(num)
#print(lstTraversedNumbers)
if(num in lstAvailableNumbers):
#print('Inside if condition for num in lstAvailableNumbers ' )
lstAvailableNumbers.remove(num)
#print(num)
if(num in lstTraversedNumbers):
#print('Inside if condition for num in lstTraversedNumbers ' )
#print(num)
lstDuplicateNumbers.append(num)
lstIndexPosition =[]
lstIndexPosition.append(item)
lstIndexPosition.append(colItem)
dictDuplicateNumberIndex[num]=lstIndexPosition
lstTraversedNumbers.add(num)
#print(lstTraversedNumbers)
else:
lstDuplicateNumbers.append(num)
lstIndexPosition =[]
lstIndexPosition.append(item)
lstIndexPosition.append(colItem)
dictDuplicateNumberIndex[num]=lstIndexPosition
i=0
#print("Available Numbers -")
#print(lstAvailableNumbers)
#print("Traversed Numbers -")
#print(lstTraversedNumbers)
#print("Duplicate Numbers -")
#print(lstDuplicateNumbers)
#print("Duplicate Number index -")
#print(dictDuplicateNumberIndex)
for item in lstAvailableNumbers:
itemToReplace= lstDuplicateNumbers[i]
value= dictDuplicateNumberIndex[itemToReplace]
s[value[0]][value[1]] = item
i+=1
cost += abs(itemToReplace - item)
#print(cost)
return cost
*Note i refer to thing as a matrix but it is not, it is just a collection of 1's and zeros
Suppose you have a matrix that is always square (n x n). Is it possible to determine if there exists a single column/row/diagonal such that each item is a 1.
Take the matrix below for instance (True):
1 0 0
1 1 0
0 0 1
Another example (True):
1 1 1
0 0 0
0 1 0
And finally one without a solution (False):
0 1 1
1 0 0
0 0 0
Notice how there is a diagonal filled with 1's. The rule is there is either there is a solution or there is no solution. There can be any number of 1's or zeros within the matrix. All i really need to do is, if you have (n x n) then there should be a row/column/diagonal with n elements the same.
If this is not possible with recursions, please let me know what is the best and most efficient method. Thanks a lot, i have been stuck on this for hours so any help is appreciated (if you could post samples that would be great).
EDIT
This is one solution that i came up with but it gets really complex after a while.
Take the first example i gave and string all the rows together so you get:
1 0 0, 1 1 0, 0 0 1
Then add zeros between the rows to get:
1 0 0 0, 1 1 0 0, 0 0 1 0
Now if you look closely, you will see that the distances between the 1's that form a solution are equal. I dont know how this can be implemented though.
In search for an elegant solution I came up with this:
class LineOfOnesChecker(object):
_DIAG_INDICES = (lambda i: i, lambda i: -i - 1)
def __init__(self, matrix):
self._matrix = matrix
self._len_range = range(len(self._matrix))
def has_any(self):
return self.has_row() or self.has_col() or self.has_diag()
def has_row(self):
return any(all(elem == 1 for elem in row)
for row in self._matrix)
def has_col(self):
return any(all(self._matrix[i][j] == 1 for i in self._len_range)
for j in self._len_range)
def has_diag(self):
return any(all(self._matrix[transf(i)][i] == 1 for i in self._len_range)
for transf in self._DIAG_INDICES)
Usage:
print LineOfOnesChecker(matrix).has_any()
You can have a list of n 1's and do an 'AND' for your sets of diagonal elements, row elements and column elements and if any of those AND operation results in a TRUE, then you have your valid pattern.
import sys
matrix = [[1,0,1],[1,0,1],[1,0,1]]
transpose = zip(*matrix)
diagonal1 = []
for n,elem in enumerate(matrix):
diagonal1.append(elem[n])
diagonal2 = []
for n,elem in enumerate(transpose):
diagonal2.append(elem[n])
for row in matrix:
if reduce(lambda x,y: x and y, row):
print True
sys.exit()
for row in transpose:
if reduce(lambda x,y: x and y, row):
print True
sys.exit()
if (reduce(lambda x,y: x and y, diagonal1) or reduce(lambda x, y: x and y, diagonal2)):
print True
sys.exit()
From what I understand of the problem, you just need to check whether any row, coloumn or diagonal consists entirely of '1's. This can be done very easily using all in Python, so I don't get why you want to do this recursively.
The more obvious solution (in my mind) is something like this:
#! /usr/bin/env python
boards=[
((0,1,0),(1,0,1),(0,1,0)),
((1,1,1),(0,0,0),(0,0,0)),
((0,0,0),(1,1,1),(0,0,0)),
((0,0,0),(0,0,0),(1,1,1)),
((1,0,0),(1,0,0),(1,0,0)),
((0,1,0),(0,1,0),(0,1,0)),
((0,0,1),(0,0,1),(0,0,1)),
((1,0,0),(0,1,0),(0,0,1)),
((0,0,1),(0,1,0),(1,0,0)),
((0,0,0),(0,0,0),(0,0,0))
]
def check(board):
for row in board:
if all(row):
return True
for col in xrange(len(board)):
vector=[board[row][col] for row in xrange(len(board))]
if all(vector):
return True
diag1=[board[i][i] for i in xrange(len(board))]
if all(diag1):
return True
diag2=[board[i][i] for i in xrange(len(board)-1,-1,-1)]
if all(diag2):
return True
return False
if __name__=='__main__':
for board in boards:
if check(board):
print "Yes"
else:
print "No"