2

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.

0

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

Did you actually test this?
@SQL Police: I've added some tests to demonstrate routines in action
This has become now a great answer!
@SQL Police: thank you! Downvotes really make me think again ;)
I think you can merge the validationchecks: if (null == list || list.Count == 0 || from >= list.Count) return ""; Also the ToString() in de last line is Redundant
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.