I’ve been following the Python course, everything went fine until I came to the DFS section. I couldn’t solve this and I was completely lost. I tried for like 2 weeks and a miracle happened, my code passed. But here I am, the N-queens problem ! I’ve been struggling for 1~2 months and I’m still completely lost. I didn’t understood at all how to make algorithms and I see the next part is about algorithms again
. If you have any advices, please help me.
Please post a link to the challenge so we know what you’re talking about.
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.
So the challenge that I’m talking about is :
Implement the N-Queens Problem: Implement the N-Queens Algorithm | freeCodeCamp.org
but I’m not really in trouble with this challenge specifically, but with all algorithms (in a generic way).
Stick with it. One step at a time. You’re close to the finish line! ![]()
If i get stuck for more than an hour on the problem, it just means there’s an easier way to solve this and my intellect is simply not capable to produce this. So, the only thing i can do is reinforcement. I ask chatgpt the solution. Understand the logic. Copy. Then, do it again by myself to locate exactly where i’m getting stuck. Repeat until I feel confident that if I face the problem again, I have some idea of what is needed. Ik memorization is playing a huge part here, but it’s all part of it. You’ll have one or two eureka moments.
It’s kinda asking an elementary school kid to write an essay. He most probably won’t be able to do it. You give him a picture and tell him to write 10 sentences. The next time he is asked to write an essay, he knows how to go about solving that problem. That’s my view of it.
Okay thanks, I’ll try that ! ![]()
Don’t do this. Uncountable comments on this forum reporting that CGPT has mis-lead them. Not to mention the fact that this is bascially checking the answers at the back of the book, except you’re not sure if they are right or not.
Learning is hard, it takes time and sweat. There are no shortcuts.
Yes, I noticed that! I tried it multiple times and like every time it was saying “There is one last bug. Fix this” and then said “There is one last bug. Change this to that” etc
It was SOOO annoying so I asked you !
Edit : Chat GPT or any other AI cannot interpret any code. Don’t trust them.
Do some other research on algorithms. Don’t be afraid to do a web search for more information on algorithms, more research on DFS implementations, maybe some youtube videos to understand it better. Try different sources and approaches.
I agree this one is hard. I was also stuck on it so I skipped it to think on it and moved ahead to the other problems. While I was working on one of the later problems I had an idea to get further on this one.
Try and write something that at least, a partial solution.
when you get stuck, it’s the same as any other problem you share on the forum. Share your code and a link to the challenge. Talk about how you are stuck and share the thinking behind your approach.
Another good exercise is to write out in words what your program will need to do and the steps it will need to follow.
I’m currently reviewing the N-Queens problem Geeks4Geeks page, I think I understand it better but there is one thing that I can’t get : recursion. It’s :
# Consider the row and try placing
# queen in all columns one by one
for i in range(n):
# Check if the queen can be placed
if isSafe(mat, row, i): #This is a function that checks columns and diagonals
mat[row][i] = 1
placeQueens(row + 1, mat, result)
# backtrack
mat[row][i] = 0
The 2 last lines. I really can’t get this.
Edit : In fact just check the Geeks4Geeks recursion page. I just need every time I am in trouble go check G4G. Bonus for this page that is the same but in Python.
I also find that site has very helpful explanations and examples
You could use recursion here, but if it’s stopping you from implementing a solution, there’s no requirement to use recursion at all.
Try to make something in the simplest way you see to solve the problem. Don’t need to be fancy unless you clearly understand how it will help.
I first thought “Let’s generate every single possibility then eliminate the ones that don’t fit !” but I showed it to Copilot and it told me to use recursion. My first idea was inefficient and really ressource-consuming (I think servers could crash if I ask them to generate 8^8 possibilities), so I decided to change, but should I have kept it ?
Edit : Just found this (on G4G
) !
When to Avoid Recursion
When the problem can be solved easily with loops.
When recursion depth is large enough to risk a stack overflow.
When performance is critical and function call overhead matters.
Guess what I will answer.
You are relying on co-pilot too much and it’s stopping you from thinking for yourself. Co-pilot can probably write a solution to this by itself. If that’s what you want to do, go ahead. But, if you want to learn programming, don’t use co-pilot.
Also, I just said you don’t need to use recursion. There’s no requirement in the tests that you need to use recursion.
I did something very similar.
Do the instructions say that you need to write the most efficient possible solution?
Is recursion always more efficient?
Worry about that if your program is stalling. Don’t try to optimize your program before you even start writing it.
Just write something that works based on what you’ve learned so far.
It was quite (8^8 is about 16 700 000, so creating n^n lists of n length …)
What do you mean when you say “every single possibility” ?
Is there something easy you can do or check to cut the number of possibilities down?
For example, two queens cannot share the same row or column.
That is true, but my first approach was : “Generate every single possibility (for n = 4 : [0, 0, 0, 0], [0, 0, 0, 1], … , [4, 4, 4, 4]) then delete if it is on the same row, if it is on the same column, on the same diagonal”. That is why I said it was inefficient ![]()
try using a depth first approach:
put a Q on the first row
then put a Q on the second row, is it on the same column or diagonal as an other queen? then abort and put it on an other cell of the second row.
Then put a queen on the third row, is it invalid? if it’s invalid, change cell
and so on
if one row does not have valid cells, abort and go change cell on a row above
if you reach the eighth row and it’s valid, save the combination.
then go back one row and find if there is an other valid combinations
if there is no valid combination remaining go back one row and change queen position, if there is a valid combination go down one row and try putting queens there
on the eighth row, save the valid one
