<jupyter_start><jupyter_text><jupyter_code>nQueens = 8
currentSolution = [0 for x in range(nQueens)]
solution = []
def isSafe(tRow, tCol):
  if tRow == 0:
    return True

  for row in range(0, tRow):

    #check vertical
    if tCol == currentSolution[row]:
      return False

    #check diagonal
    if abs(tRow - row) == abs(tCol - currentSolution[row]):
      return False

  # no attack found
  return True
    
for queen0 in range(nQueens):
  if not isSafe(0, queen0):
    continue
  else:
    currentSolution[0] = queen0

  for queen1 in range(nQueens):
    if not isSafe(1, queen1):
      continue
    else:
      currentSolution[1] = queen1

    for queen2 in range(nQueens):
      if not isSafe(2, queen2):
        continue
      else:
        currentSolution[2] = queen2
      
      for q3 in range(nQueens):
        if not isSafe(3, q3):
          continue
        else:
          currentSolution[3] = q3

        for q4 in range(nQueens):
          if not isSafe(4, q4):
            continue
          else:
            currentSolution[4] = q4

          for q5 in range(nQueens):
            if not isSafe(5, q5):
              continue
            else:
              currentSolution[5] = q5

            for q6 in range(nQueens):
              if not isSafe(6, q6):
                continue
              else:
                currentSolution[6] = q6

              for q7 in range(nQueens):
                if not isSafe(7, q7):
                  continue
                else:
                  currentSolution[7] = q7
                  solution.append(currentSolution.copy())



print(len(solution), "Solution Found!")
for sol in solution:
  print(sol)<jupyter_output><empty_output><jupyter_text>**Alternative Solution**<jupyter_code># or we can run this function instead
nQ = 4
cs = [0 for x in range(nQ)]
s = []

def isSafe(tRow, tCol):
  if tRow == 0:
    return True

  for row in range(0, tRow):

    #check vertical
    if tCol == cs[row]:
      return False

    #check diagonal
    if abs(tRow - row) == abs(tCol - cs[row]):
      return False

  # no attack found
  return True


def setQ(row):
  global cs, s, nQ

  for col in range(nQ):
    if not isSafe(row, col):
      continue
    else:
      cs[row] = col
      if row == (nQ -1):
        s.append(cs.copy())

      else:
        setQ(row + 1)  

setQ(0)<jupyter_output><empty_output><jupyter_text>**[5, 2, 0, 6, 4, 7, 1, 3]**For this 8-queen solution,\**Q is safe in:**\`0th col 5th row`\`1st col 2nd row`\`2nd col 0th row`\`3rd col 6th row`\`4th col 4th row`\`5th col 7th row`\`6th col 1st row`\`7th col 3rd row`<jupyter_code>print(len(s), "Solution Found!")
for sol in s:
  print(str(sol) + '\n')<jupyter_output>2 Solution Found!
[1, 3, 0, 2]

[2, 0, 3, 1]<jupyter_text>**[1, 3, 0, 2]**For this 4-queen problem,\Q is safe in:0th col 1st row\1st col 3rd row\2nd col 0th row\3rd col 2nd row<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11738535249828884, 'fraction_numerical': 0.03353867214236824, 'mean_word_length': 2.1531823085221142, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '470121', 'license': 'MIT', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1028, 'n_words': 302, 'path': '8QueenProblem.ipynb', 'repo_name': 'abidshafee/python_tips', 'size': '2922'}