I had some recursive problems to solve (and after some struggle I ended up victorious) I made the functions (that calculates the result for them) void and after every iteration of the recursive function I called "Console.WriteLine()" to print the line and after I continued with the recursion until a certain condition was met afterwards I called the calculation function into the Main function. Now I want to try to convert them into strings and make them cleaner and then call the function inside Main with Console.WriteLine(). For example the following code.
public static void TowerOfHanoi(int n, char firstRod, char secondRod, char thirdRod)
{
if (n == 0)
{
return;
}
TowerOfHanoi(n - 1, firstRod, thirdRod, secondRod);
Console.WriteLine("From " firstRod " to " secondRod);
TowerOfHanoi(n - 1, thirdRod, secondRod, firstRod);
}
This is for the Tower of Hanoi problem where I have to move a whole stack of discs from a certain rod to another(in my problem from A to B) using using an intermediary rod C. The rules: I can't have a disc larger on top of a smaller one, so every move I make has to keep the smaller discs above the bigger ones.
I tried to change the function from void to string and add a new empty string in there for example:
string result = ""; and instead of the Console.WriteLine I tried using result = "From " firstRod " to " secondRod "\n"; and afterwards call the function into Main using Console.WriteLine. => Here I hit wall.
This is my failed attempt to convert it: If I enter 2 as input it should return:
From A to C From A to B From C to B.
but with my failed attempt it returns only From A to B. I know that I should have a variable where I should add all the results after each recursion, but I can't seem to figure it out
public static string TowerOfHanoi(int n, char firstRod, char secondRod, char thirdRod)
{
if (n == 0)
{
return "";
}
TowerOfHanoi(n - 1, firstRod, thirdRod, secondRod);
TowerOfHanoi(n - 1, thirdRod, secondRod, firstRod);
return "From " firstRod " to " secondRod "\n";
}
Do you have any suggestions on how can I get around this? Thank you in advance!
CodePudding user response:
Your updated code significantly changes the logic of the original code in two ways:
- The "from ... to" result is now after both of the recursive calls, whereas previously it was between the two recursive calls.
- The result from those recursive calls is entirely ignored. If a method returns a result, and you want that result, you have to capture it in some way.
For example:
var result = string.Empty;
if (n == 0)
{
return result;
}
result = TowerOfHanoi(n - 1, firstRod, thirdRod, secondRod);
result = "From " firstRod " to " secondRod "\n";
result = TowerOfHanoi(n - 1, thirdRod, secondRod, firstRod);
return result;
You can then start to improve upon the functionality a little bit by using a StringBuilder
instead of appending strings:
if (n == 0)
{
return string.Empty;
}
var result = new StringBuilder();
result.Append(TowerOfHanoi(n - 1, firstRod, thirdRod, secondRod));
result.Append("From " firstRod " to " secondRod "\n");
result.Append(TowerOfHanoi(n - 1, thirdRod, secondRod, firstRod));
return result.ToString();