4

I can’t seem to understand what is wrong with the following Markdown content. I keep getting the

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

My Markdown content looks like this:

I'm solving this IDDFS-based problem where I am using a utility function that performs recursive DFS based on depth. I have written the Python code for it which works perfectly, but I have been asked to convert it to C++, wherein the problem occurs.

Let me explain with the help of an example graph that I am using where there are 7 nodes labeled from 0 to 6. Their adjacency list is as follows:\
0: [4],\
1: [4, 5],\
2: [3, 5],\
3: [2, 6],\
4: [0, 1, 5],\
5: [1, 2, 4],\
6: [3]\
And I am attempting to find the DFS between 4 and 2 with a depth of 2, whose answer should come out to [4,5,2].

Now, I will attach the working Python code along with its output:

def dfs_util(path,target,depth):
    # Returns path if it exists, None otherwise
    curr_node = path[-1]
    if curr_node == target:
        return path
    if depth<=0:
        return None
    for child in adj_list[curr_node]:
        print(child)
        new_path = list(path)
        new_path.append(child)
        result = dfs_util(new_path,target,depth-1)
        # Remove this print statement gurllllll
        print(new_path, result)
        if result is not None:
            return result
    return None

I have added the extra print statements in there to help debug and understand the flow. The output this gave for command ```dfs_util([4],2,2)``` is:\
0\
4\
[4, 0, 4] None\
[4, 0] None\
1\
4\
[4, 1, 4] None\
5\
[4, 1, 5] None\
[4, 1] None\
5\
1\
[4, 5, 1] None\
2\
[4, 5, 2] [4, 5, 2]\
[4, 5] [4, 5, 2]\
[4, 5, 2]

As we can see, this returns the correct output.

When I tried to convert this to C++, I wrote the following function:

vector<int> dfs_util(vector<int> path, int target, vector<vector<int> > adj_list, int depth){
        int curr_node = path.back();
        if(curr_node == target)
            return path;
        if(depth<=0){
            vector<int> tmp;
            tmp.push_back(NULL);
            return tmp;
        }
        for(auto child : adj_list[curr_node]){
            cout<<child<<endl;
            vector<int> new_path = path;
            new_path.push_back(child);
            vector<int> result = dfs_util(new_path, target, adj_list, --depth);
            cout<<"[";
            for(auto i : new_path)
                cout<<i<<" ";
            cout<<"]\t";
            cout<<"res=";
            for(auto i : result)
                cout<<i<<" ";
            cout<<"\n";
            if(result.back()!=NULL)
                return result;
        }
        vector<int> tmp;
        tmp.push_back(NULL);
        return tmp;        
    }

With the same input parameters, it should ideally return the same output as the Python code mentioned above. But instead, it prints the following:
0\
4\
[4 0 4 ]        res=0 \
[4 0 ]  res=0 \
1\
[4 1 ]  res=0 \
5\
[4 5 ]  res=0 

While chalking out the logic, I have come to understand that after entering the recursion loop where ```dfs_util([4,1],2,1)``` is called, it does not enter the for loop mentioned further, thus not exploring the edges connected to node 1.

I have been stuck on this for two days now and would really appreciate some help in figuring out what is going wrong here since I feel like it should technically work (at least it does when I try to go line by line and solve on paper).

What is the issue?

11
  • 2
    What are all those lines starting with numbers (or a number in square brackets)? You should probably use code blocks there.
    – Laurel
    Commented Feb 17, 2022 at 13:32
  • They are outputs. Should they be in code blocks too?
    – imtryin123
    Commented Feb 17, 2022 at 13:33
  • Ok, just tried that. I still get the error.
    – imtryin123
    Commented Feb 17, 2022 at 13:36
  • You should remove the backslashes from after the colons too. Other than those 3 blocks of output, I don't see anything else that looks suspicious. You could try putting [4,5,2] in backticks but at this point it's just a wild guess.
    – Laurel
    Commented Feb 17, 2022 at 13:44
  • 2
    Try to post a part of your question, if that fails, reduce that part, untill you succeed. Then add part by part into the question.
    – Luuklag
    Commented Feb 17, 2022 at 14:05
  • I'm stupid where code is concerned. Seriously, I know nothing. So... is this piece of information, the first line, part of the code, too? I'm solving this IDDFS-based problem where I am using a utility function that performs recursive DFS based on depth. I have written the python code for it which works perfectly but I have been asked to convert it to CPP, wherein the problem occurs. Commented Feb 17, 2022 at 16:49
  • 5
    @Mari-LouA No. OP has encased their entire question in a code block so that we can see the raw markdown they were using. I'm taking it on faith that their entire question wasn't actually in a code block when they tried posting it on Stack Overflow.
    – F1Krazy
    Commented Feb 17, 2022 at 16:59
  • 1
    @F1Krazy thank you for clearing it up. But wouldn't it be better, on MSE, for the OP to use backticks for the section that is code and leave the rest as quotes? Anyhoo, if you guys can all understand it, then I'll quietly close the door behind me. Commented Feb 17, 2022 at 17:04
  • Thank you so much guys! Putting the output numbers in code blocks worked. @Laurel can you post that as an answer so I can accept it? :)
    – imtryin123
    Commented Feb 17, 2022 at 18:13
  • It's a good idea to put any sample data into a code block, even if that data is plain text. That makes it easier for readers to see exactly what the data is, and for them to copy & paste it if they want to run your code & feed it your data.
    – PM 2Ring
    Commented Feb 17, 2022 at 19:45
  • Editor's note: please refrain from adding meta noise in your questions in the future. We are building a repository of qusstions and answers, not maintaining a forum - things like "I am stuck for 2 days" have no place in posts. Also, instead of adding notes of appreciation (which is also against the guidelines), pay it forward with voting and accepting (not sayjng you didn't, just don't add such noise to posts, please) Commented Feb 20, 2022 at 1:00

1 Answer 1

3

Putting the output numbers in code blocks worked.

The moral of the story is that any text with a lot of symbols can trip up the code detection. You should put any output in code blocks (with maybe the exception of when it's an error message in plain English).

1
  • 1
    And that error message could be put in a quote block, that should work too.
    – Luuklag
    Commented Feb 17, 2022 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .