I tried to convert a method with a loop for recursion as part of the coding practice of Kata (trying to solve problems with a recursive approach). There is nothing special in logic, but
- In the recursive method, a breakpoint does not hit anywhere.
- I tried to place a logger (console output) to check if the method was called, but nothing was logged.
Here is the definition of the method (s):
public IEnumerable<Tuple<int, int>> GetElementWithLargestDeltaOnTimeline(int[] a)
{
int runningLindex = 0;
int currLValue = a[0];
int runningHindex = 1;
int currHvalue = a[1];
int currDelta = 0;
for (int i = 1; i < a.Length - 1; i++)
{
if (a[i] < currLValue)
{
currLValue = a[i];
runningLindex = i;
}
for (int j = runningLindex + 1; j < a.Length; j++)
{
if ((a[j] - currLValue) > currDelta)
{
currDelta = a[j] - currLValue;
runningHindex = j;
currHvalue = a[j];
}
}
}
yield return new Tuple<int, int>(currLValue, runningLindex);
yield return new Tuple<int, int>(currHvalue, runningHindex);
}
Recursive -
public IEnumerable<Tuple<int, int>> GetElementWithLargestDeltaOnTimelineRec
(int[] a, int i, int j, int runningLindex, int currLValue, int runningHindex, int currHvalue, int currDelta)
{
Console.WriteLine("Iteration i-{0}: j-{1} runningLindex-{2} currLValue-{3} runningHindex-{4} currHvalue-{5} currDelta-{6}"
, i, j, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
if (i < a.Length)
{
if (a[i] < currLValue)
{
currLValue = a[i];
runningLindex = i;
}
if (j < a.Length)
{
if ((a[j] - currLValue) > currDelta)
{
currDelta = a[j] - currLValue;
runningHindex = j;
currHvalue = a[j];
}
GetElementWithLargestDeltaOnTimelineRec(a, i, j++, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
}
}
else
{
yield return new Tuple<int, int>(currLValue, runningLindex);
yield return new Tuple<int, int>(currHvalue, runningHindex);
}
GetElementWithLargestDeltaOnTimelineRec(a, i++, runningLindex + 1, runningLindex, currLValue, runningHindex, currHvalue, currDelta);
yield break;
}
Home -
public class Program
{
public static void Main()
{
var a = new[] { 10, 9, 3, 6, 7, 8, 15, 10, 6 };
var val = new StockManager();
var result = val.GetElementWithLargestDeltaOnTimelineRec(a, 0, 0, 0, a[0], 1, a[1], 0);
}
}
Question -
- Are recursive method calls something (wrong) causing the problem?
- Why the method is not called and the result is not returned without failures / errors / warnings?
Additional Information -
.Net 4.5, Visual studio 2013
( , - VS).