Eight Queens Puzzle by Magnus Lie Hetland

chansky tom_chansky at rocketmail.com
Wed Aug 13 15:57:26 CEST 2003

I've been reading Magnus Lie Hetland's book, Practical Python. In one
of the chapters he discusses how to use a Generator function to solve
the Eight Queens puzzle. The following is part of his codes:

#This function checks if the next Queen would cause any conflict with
the #previous one.
def conflict(state, nextX):
	for i in range(nextY):
		if abs(state[i]-nextX) in (0,nextY-i):
			return 1
	return 0

def queens(num,state):
	if len(state)==num-1:
		for pos in range(num):
			if not conflict(state,pos):
				yield pos

I've a hard time to understand the following statement:

if abs(state[i]-nextX) in (0,nextY-i):

The author said that this statement will be True if the horizontal
difference b/w the next queen and the previous one under consideration
is euqal to zero or equal to the vertical distance (diagonally), and
false otherwise.

Well, maybe I'm really slow. What does that "vertical distance" really
mean? If someone out there has read his book, can you help me
understand that line of code.

More information about the Python-list mailing list