Hacker News new | past | comments | ask | show | jobs | submit login

Here's my Python3 version (4 line nQueens function):

    from itertools import permutations

    def nQueens(n):
        return (p for p in permutations(range(n))
                if (len(set(c+d for c,d in enumerate(p))) == n and 
                    len(set(c-d for c,d in enumerate(p))) == n))

    for num,solution in enumerate(nQueens(8)):
        print("Solution #", num+1)
        print("\n".join(("  ".join("Q" if i == col else "-" for i in range(8))) for col in solution))



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: