دنبال کننده ها

۱۳۹۶ مهر ۸, شنبه

python - recursive function affecting variable

[ad_1]



I have a recursive function to count the number of queens that can fit on an 8x8 chess board (for a class). It works well and gives the correct permutations, the interesting thing happens when I have the program try to count the answers -it constantly returns my counter to zero. When I manually count the permutations it's 92.



def can_be_extended_to_solution(perm):
i = len(perm) - 1
for j in range(i):
if i - j == abs(perm[i] - perm[j]):
return False
return True

def extend(perm,count, n):
if len(perm)==n:
count=count+1
print "cycle counter= ",count
print(perm)

for k in range(n):
if k not in perm:
perm.append(k)
if can_be_extended_to_solution(perm): # if it works
extend(perm, count, n)
perm.pop()

extend(perm = [], count=0, n = 8)



[ad_2]

لینک منبع