简体   繁体   中英

C# recursion in list from certain index

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

  • 8 returns "8 9 10 11"
  • 5 returns "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.

Something like this

public static string ForwardString(List<int> list, int from) { 
  // Validation (since we implement public method we should be ready for any input)
  if (null == list)
    return ""; // or throw new ArgumentNullException(nameof(list));
  else if (list.Count == 0) 
    return ""; 
  else if (from >= list.Count)  
    return "";
  else if (from < 0)
    from = 0; // or throw new ArgumentOutOfRangeException(nameof(from));

  // Recursion stop: on the last item
  if (from == list.Count - 1)
    return list[from].ToString();

  // Recursion call: current item, space, then all the other items
  // i.e. "5" + " " + "6 ... 11" 
  return list[from].ToString() + " " + ForwardString(list, from + 1);
}

In case of BackwardString you have to change Recursion call:

public static string BackwardString(List<int> list, int from) { 
  // Validation (since we implement public method we should be ready for any input)
  if (null == list)
    return ""; // or throw new ArgumentNullException(nameof(list));
  else if (list.Count == 0) 
    return ""; 
  else if (from >= list.Count)  
    return "";
  else if (from < 0)
    from = 0; // or throw new ArgumentOutOfRangeException(nameof(from));

  // Recursion stop: on the last item
  if (from == list.Count - 1)
    return list[from].ToString();

  // Recursion call: all the other items, space, then current item 
  // i.e. "11 ... 6" + " " + "5" 
  return BackwardString(list, from + 1) + " " + list[from].ToString();
}

Let's run some tests (with a pinch of Linq ):

using System.Linq;

...

List<int> data = Enumerable.Range(0, 12).ToList();

var result = Enumerable
  .Range(0, 12)
  .Select(i => $"{i,2} fwd: {ForwardString(data, i),-25} bwd: {BackwardString(data, i)}");

string report = string.Join(Environment.NewLine, result);

Console.Write(report);

Outcome:

 0 fwd: 0 1 2 3 4 5 6 7 8 9 10 11 bwd: 11 10 9 8 7 6 5 4 3 2 1 0
 1 fwd: 1 2 3 4 5 6 7 8 9 10 11   bwd: 11 10 9 8 7 6 5 4 3 2 1
 2 fwd: 2 3 4 5 6 7 8 9 10 11     bwd: 11 10 9 8 7 6 5 4 3 2
 3 fwd: 3 4 5 6 7 8 9 10 11       bwd: 11 10 9 8 7 6 5 4 3
 4 fwd: 4 5 6 7 8 9 10 11         bwd: 11 10 9 8 7 6 5 4
 5 fwd: 5 6 7 8 9 10 11           bwd: 11 10 9 8 7 6 5
 6 fwd: 6 7 8 9 10 11             bwd: 11 10 9 8 7 6
 7 fwd: 7 8 9 10 11               bwd: 11 10 9 8 7
 8 fwd: 8 9 10 11                 bwd: 11 10 9 8
 9 fwd: 9 10 11                   bwd: 11 10 9
10 fwd: 10 11                     bwd: 11 10
11 fwd: 11                        bwd: 11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM