I was doing some exercises about recursion but I stumbled upon one which I just cannot seem to think of a solution for.
List<int> list = new List<int>(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
so the first one was using the above list create a method:
public string ForwardString(List<int> theList, int from)
in which the recursive function returns a set of numbers based onto the index that was given so
8returns"8 9 10 11"5returns"5 6 7 8 9 10 11"
etc. okay piece of cake.
public static string ForwardString(List<int> list, int from)
{
if (from >= list.Count || list.Count == 0)
return "";
if (list.Count - 1 == from)
return " " + list[from];
return (" " + list[from]) + ForwardString(list, (from + 1));
}
but then there is also one for backwards:
public string BackwardString(List<int> theList, int i)
so you enter 5 and the output should be "11 10 9 8 7 6 5" but I can't seem to find a solution for this I tried adding to the front of a string but that yielded in the same result as above method. And for the life of me I simply can't come up with a good method.