Recursion, help finding the base case?
I think the idea is that if c is 1 letter, it is the base case and then after it increases. Not sure exactly how to implement it, very new to coding
44 Replies
Do it with a loop, first
for (int i = 0; i < n; i++)
{
string Result = Repeat(c,n);
}
I think something like this
No, not really
This is mixing recursion with loops
Just use a loop
string.Empty = Result
for (int i = 0; i < n; i++)
{
Result += C;
}
return Result; ?
Almost
😦 whats the error
string.Empty = Result
Hmm I'm a bit lost
type name = value
Not value = name
Result = string.Empty ?
Assuming
Result
variable already exists
If not, it's missing a typeWhat do you mean by type?
The type of the variable
C# is a statically and strongly-typed language
Everything has a type
It could be an
int
, or a bool
, or a DateTime
or a Person
Result can be a string then right?
Sure
Now we have to look at recursion
I have to practice because I am a bit lost with that
You seem to be lost with C# in general
So, yeah, true
Yeah ðŸ˜
It's rough out here
Well, the general gist of recursion, is that it's a method that calls itself and uses its own return value
Yeah
My teacher did an example with factorial that helped me
Bascially when it was 0 or 1 it was the special case to where it stored those values on the stack for cases so it could trace back to it
So like factorial 2 would use the base case of factorial 1 times 2
then 3 would take from prior
Yep
So, adding to string should be even easier
Each iteration of the method would simply return a string one longer
Until a desired lenght is reached
Then the method will not recurse
Yeah
@ZZZZZZZZZZZZZZZZZZZZZZZZZ Here is how my teacher did it
So we had a base case of 1
Yeah
Adapting it to a string should be easy, then
a base of an empty string
And instead of multiplying, you'd be adding
I'm a bit confused on how to format the if statement thoug
I said earlier the base case is like
if you have 1 letter it is the base case
if not you increase
Yeah, that
im just a bit confused
on how to implement the return
if (theString.Length < n)
Or > n
Or whichever else worksWouldn't it be = ?
== 1
If you want to check exact equality, sure
Probably not
1
thoughActually I don't even fully understand the base case
Hmmm
Well, you need to start with something
I was thinking if it was 1 character but really I don't know what it would return - like for factorial I know that if you do factorial 1 or 0 it is just 1 and then AFTER you can extend off that
I guess if you have 1 character you can always draw back to that?
You can have zero characters
That's what
string.Empty
is
Or an empty string like ""
I guess that could maybe be a basecase too
should i make 2 if statements?
Why?
for the two base cases, if they have 0 or 1 character
Why would there be two?
You start with an empty string
Then you keep adding to it
Until its length reaches a desired length
One base — empty string
One check — string reached the desired length
So if n == 0; I will return an string.empty string?
Yeah
Okay
I hav this dowm, is it okay?
@ZZZZZZZZZZZZZZZZZZZZZZZZZ now i do an e;se statement saying return c + Repeat(c + 1) ?
Yep
wow
I am actually choked
shocked
thank you
i cant believe i kinda got one
The else statement is redundant, because if
n != 0
it will simply execute the code after the if (n == 0)
statementthis was the correct ans, got it wrong