#Recursion
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @dense anvil! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
I have a good chunk of the code done
Ill post it here
import java.util.Scanner;
public class Exercise{
//this will act as the main recursive method for reversing a string
public static String reverse(String str){
{ //Base or stop condition
return str;}
else{
return str + str.charAt(0);//recursive call
}
}
public static void main (String[] args){
Scanner reader = new Scanner(System.in);
String str= "This is an example";
System.out.println(reverse(str));
}
}
your method is missing a recursive call to itself i think
And a base case, and a reduction/evolution of the case
I guess first OP should discuss about what they think the base case is supposed to be
Thats how it was setup from the previous exercises/examples we did yesterday (for context the class is just a intro cor Computer science) as we were just getting started with recursive.
I just have no idea how to go about making a string into a base condition/stop condition
But concerning general ideas, do you have general ideas?
I have some
also found a example
just had to transfer it into javav, give me a moment
int factorial(int n){
if(n == 0){//base condition
return 1;
} else {
return n * factorial(n-1); //recursive call
}
}
void main(String[] args){
int n; //
System.out.println("Enter the number: ");
n= reader.nextInt(); // we assume the user enter a positive number
System.out.println("The factorial for “+ n+ “ is “+ factorial(n));
}
Yes, that's the typical example for recursion
And indeed, it's not on Strings that it operates
But do you understand it?
a little bit
I am having trouble getting the idea of how it would be done with a string
I understand the factorial for this
The idea is incrementally reducing the problem, until you get one where the response is trivial.
Can you imagine a String about which it is trivial to reverse it?
Hmm I can
excellent. When you feel you've fantasized enough about it in your head, please share it here
Okay
Alright
I think I got it
atleast for the base case
if(str == "")
not sure about the recursive call
Well now you need to find a way to take in any String, and gradually turn it into your base case
Here your base case is the empty string
Can you think of a way to, one step at a time, modify a string so that it ends up being the empty string?
not at the moment
Also if it helps I can show you what the exercise im doing (the current work im having trouble with) is as we are just filling in the blank
I have a confession to make to you: I know how to reverse a String recursively
I'm merely, at your request, trying to guide you into doing it too
Ah gotcha
But I suppose you can show the exercise anyway
yeah, I feel i phrased it in a way that its a full on working program
Ah, they force on you to append the first char to the result of the recursive call
yeah
It sounds like that force us to deal with this first character when we reduce the string
do you think we can get the trick by turning it uppercase?
hmm no I dont believe so unless im over looking something
AH, you're right, turning it uppercase doesn't reduce the case
Maybe we could double the first char, like, turning "Hello" into "HHello"?
hmm that can work
Yeah? Maybe eventually the string will be an empty string
Let's try it
Hello
HHello
HHHello
HHHHello
I wonder when will it be the empty string
hmm I feel i am mis interpeting something
give me a second
hmm that wasn't really helpful
I looked over the material and it aint that helpful
That's great, but we were in the middle of a conversation here
Apologies
So, what do you think about the idea to double the first character to reduce the case?
It can work with reducing the case but it feels like it would end up being like a loop
Really, you have the impression that this, is increasingly reducing the case?
Hello
HHello
HHHello
HHHHello
No
Now that I think about it
it will just keep adding onto the case instead of reducing it
Ah, right
So, what could we do to this first character...
Tried turning it uppercase, doesn't really help
Tried doubling it, doesn't really help
What else, what else...
Maybe we could swap it with the next character?
hmm maybe
Let's try that, Like when I tried doubling it on the example "Hello"
Can you show here what it does?
I can give it a try
Hello people, can I join in to help?
uh sure?
Correct
mhm now what are some important things in a recursive function
There are 2 main components
One is the base case
the base and the recursive call
lovely!
just don't know how to do that for a string
now let's look at the function "public static String reverse"
doesnt help all of our examples are of factorials
if you're reversing a string
would you need to reduce the string
as the function goes on
Like my first call of "Hello" would be
elloH and then
lloHe
right? if you're trying to reverse it
Right
mhm so in the recursive calls, you would need to reduce the string length as you're passing it
and the base would check when the str.length() == 1, then you stop the recursive function
Let's think of it this way:
The code says ____ + str.chatAt(0)
If I say "Hello", it will say "HelloH"
this would be the case for reverse(str) + str.chatAt(0)
but now, if I say reverse(str.subString(1)) + str.charAt(0)
What do you think is the output (assuming it runs one time)
Hm? Why? Isn't the str.chatAt(0) after the string?
what will be the output of str.subString(1)
assuming you say String h = str.subString(1);
System.out.println(h);
Don't worry it's just a simple question, like general question of what will the value be
this feels like its such a obvious answer but my mind is failing me with it
How does the subString function with one param work? It starts from that index right?
Right
So "Hello" would be ?
ello
it would be Elloh
Careful, it would be elloH but you know the idea!
Now, here's the tough part:
what if I make it do this again and again
reverse(str.subString(1)) + str.charAt(0)
the INITIAL reverse function will have
"ello" + "H"
but if I put "ello" in the reverse function
what happens?
if I say reverse("ello")
will it be `"llo + e"?
it would print it out as
"llo" + "e"
oooo see you're getting the hang of it
now now now, let's say you're at the last case
which is o + str.charAt(0)
but we know that str.charAt(0) IS o
so your string would become oolleH
Which is wrong, meaning the "base case" shouldn't return the last string left
You get what I'm saying?
yeah
What do you think should the base case be
hmm i wanna say
(str == str.subString(1))
It has something to do with str's length
that's true
but maybe think about when the function should end
maybe when there's no characters left in str?
yeah, as that would make sense so hmm
I wanna say str.length() == 1 since if it keeps repeating we will eventually.. nvm
we would just be left with the (0)
I would use <= 1 but yeah, that looks right!
Man this is way better than having to scour the internet for examples to even get a idea on it
so basically it's if(str.length() == 1){}
Lol recursion is tough
anyways, so assuming if we know the base case is when there's no length left in str
yeah and I feel string is more difficult
mainly because all i find is factorial recursion
how should we continue with the recursive calls?
factorial isn't easy either tho
Aye but it has alot of explanation on it from what I came across
also hmm I wanna say by doing like we talked about earlier
ig
so like by doing something like return(str.subString(1)) + str.charAt(0)
hm...
actually I think it should be str.subString(0, str.length()-1)
(I'm changing ways a little)
and you add the last character
thats fine! also sounds interesting with adding the last character
damn I'm so tired rn
wait let me think this through
how to explain
ok so here's the idea:
if we add the last character, let's say it's "Hello"
then we have Hell + o
Hel + ol
He + oll
H + olle
- olleH
you see what I mean?
the base case is still the same though
that makes sense
you're just going to pass in reverse(str.subString(0, str.length()-1)) + str.charAt(str.length()-1);
cuz you're shortening the string input as it goes and just plucking the last char and making it first into the string
I'm working through it as we speak, and it works like intended
i am trying to think of what else to say instead of "That makes sense"
because I feel like I said that so many times
lol it's ok
but thank you for helping me understand recursion
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
this has been extremely helpful
what is it?
aha, woops I meant str.charAt(str.length()-1) + reverse(str.subString(0, str.length()-1))
cuz you're adding it first not last 💀
or else that's just returning the original string lmao
lmao that fine!
awesome! glad to be of help
also I reccomend a course in recursion
Recursion is a powerful technique that helps us bridge the gap between complex problems being solved with elegant code. Within this course, we will break down what recursion is, why you would and wouldn’t want to use it and look at a variety of examples for how it can be used.
We’ll break down recursion with all sorts of data-structures, animat...
This was very useful for me! (you can choose to ignore it)
Thank you!
I will make sre to watch this