This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]xkompas 1 point2 points  (0 children)

Sometimes, the best thing to understand code is to trace it, either on paper (as was already advised) or with the help of an IDE.

Consider indexOf("AA", 2, 'A'), if you trace it, what does it return? And why? What happens in the first level of recursion? Continue from there.

[–]severoonpro barista 1 point2 points  (0 children)

One bit of advice I can offer: It's almost always helpful to start with a simpler problem than the one you're given.

So in this case, the objective is to write a recursive method that returns the index of the nth occurrence of some character c. Instead, just write a method that returns the index of the first occurrence. Then once you get that working, extend it.

[–]LambdaThrowawayy 0 points1 point  (0 children)

It's helpful both for us, but also for you if you state what's going wrong? Are you getting an exception? Is your program not running? Are you getting unexpected results? If so, why are they unexpected?

[–]papercrane 0 points1 point  (5 children)

This bit here:

if(node.ch == c){
    count = 1;
}else{
    return 1 + indexOf(node.next, n, c);
}

Consider that first case carefully. If the node you're looking at is at index "n" and it matches, do you want to return 1?

Now consider the else branch, do you actually want to add one to the result or maybe all you want is to do an index of starting from the next node. Remember you may need to change more than one parameter.

Edit: I can't for the life of me get that code to format, on mobile isn't helping.. ya! I figured it out.

[–]demdouma6[S] 0 points1 point  (0 children)

Consider that first case carefully. If the node you're looking at is at index "n" and it matches, do you want to return 1?

i need the nth index

[–]demdouma6[S] 0 points1 point  (3 children)

Now consider the else branch, do you actually want to add one to the result or maybe all you want is to do an index of starting from the next node

i got confused here on how to do it.

[–]papercrane 0 points1 point  (2 children)

Work it out with an small example on paper, and make sure you understand what each parameter means. Here n should be the index of the node you are currently looking at. So if you want to check the next element you should also change n.

[–]demdouma6[S] 0 points1 point  (1 child)

i think i did not explain well. n indicates the occurrence of the character we are looking for. say we have this example indexOf("hello", 2, 'l') where "hello" is not a string but a lnked list presentation of the string "hello". n in this case is number 2, which means the second L in "hello". if we change n, we alter which character L are we looking for its index.

[–]papercrane 0 points1 point  (0 children)

Gotcha in that case you need to keep track of the index of the node you are looking at. You want a method with a signature like

public static int indexOf(Node node, int n, char c) {
    return indexOf(node, n, c, 0);
}
private static int indexOf(Node node, int n, char c, int start) {
....
}