+ 2
C# Recursive - need some help
Hi everyone, Hope everyone is allright, and thanks if you have a few minutes for me. I am trying to understand recursive functions. The idea is clear, the examples I see are clear, but I gave myself a real life goal and I can't solve it. So... not that clear after all ! I say I have an array of someting (I use generics) and I want to offset it by some number to the left. So 1-2-3 becomes 2-3-1 if the offset is one, or 3-1-2 if the offset is 2, etc. I failed at writing it recursivly, how would you guys do it ? I have written it succesfuly with a base function offseting by one and an other function calling it with a for loop, but its not a success as its not recursion. Thanks in advance and take care ! P.
6 Respuestas
+ 2
Pierre I cleaned up your original code to establish a version that was much more simplified and would be easier to compare when reviewing the recursive version.
Let me know if you have any questions on how I converted the ShiftLeft(...) method into a recursive loop.
ShiftLeft - Loop Revised
https://code.sololearn.com/c7kE3akiymir/?ref=app
ShiftLeft - Recursive
https://code.sololearn.com/c1GE5RVGoVeG/?ref=app
+ 2
Pierre My pleasure... I'm happy to help. Also, thanks for the feedback.
I'm always looking for ways to help people who are obviously putting in the effort to break through to the next level.
I was impressed with how you were challenging yourself with a new concept and then reached out for help.
Your code isn't as bad as you think. There's a lot that will improve over time and with practice.
Also... code on mobile devices generally look horrible compared to using a full blown IDE or text editor on a desktop. I've had to adjust the way I look at and organize code as well in the SoloLearn App.
Keep up the good work and let me know if you need me to clarify anything.
+ 1
David Carroll thanks a LOT for your time and this double feedback.
I first understand my code is terribly ugly ... :)
Then I see some patterns I don't fully understand in both the recursive logic nd the loop one, so it's an amazing source for me to learn from well written code
I will study that, and let you know,
Thanks again
Take care
Pierre
0
Here is what I did without recursion :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//int[] int_ar = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};
int[] int_ar = {1, 2, 3, 4, 5, 6, 7, 8,9 , 10, 11};
int shiftBy = Convert.ToInt32(Console.ReadLine());
int[] result = ShiftArrayLeftBy(int_ar, shiftBy);
foreach(int x in result)
{
Console.Write(x + " ");
}
}
// Base function operating an shift to the left
private static T[] ShiftArrayLeft<T>(T[] in_array)
{
T[] out_array = new T[in_array.Length];
T tempo_value = in_array[0];
for(int i = 0; i<in_array.Length-1; i++)
{
out_array[i] = in_array[i+1];
}
out_array[in_array.Length-1] = tempo_value;
return out_array;
}
// Calls the previous function in a for loop
public static T[] ShiftArrayLeftBy<T>(T[] in_array, int nb)
{
if(nb < 1)
{
return in_array;
}
if(nb == 1)
{
return ShiftArrayLeft<T>(in_array);
}
for(int i = 0; i<nb; i++)
{
in_array = ShiftArrayLeft<T>(in_array);
}
return in_array;
}
}
}
0
Example of something I tried an does not work
(I guess because my base case returns an array and not an array + a number)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
//int[] int_ar = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};
int[] int_ar = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int shiftBy = 3;
int[] result = ShiftArrayLeftBy(int_ar, shiftBy);
foreach (int x in result)
{
Console.Write(x + " ");
}
}
public static T[] ShiftArrayLeftBy<T>(T[] in_array, int nb)
{
if (nb < 1)
{
return in_array;
}
if (nb == 1)
{
T[] out_array = new T[in_array.Length];
T tempo_value = in_array[0];
for (int i = 0; i < in_array.Length - 1; i++)
{
out_array[i] = in_array[i + 1];
}
out_array[in_array.Length - 1] = tempo_value;
return out_array;
}
return ShiftArrayLeftBy<T>(in_array, nb-1);
}
}
}
// DOES NOT WORK, RETURNS AN ARRAY BY ONE ONLY
0
@David Carroll As agreed, some feedback after doing my homework :
RECURSIVE : I was close ! (I am proud it was not that wrong in my first shot). My mistake was returning the original input array, instead of the new modified array. So it could run a 100 time it was anyway performing the operation on the base array only ! Silly me
Appart from that I see your syntax. It is a lot more concise, even if harder to read at first, I guess the chained operations with dots become the fastest and clearest way to write with experience.
I wouldn't have thought about the ternary operations (x?y:z) on the indexing. It becomes abvious now that I see that it, one line for it all.
LOOP FUNCTION : Way shorter than my code ! Thanks for the example.
It's funny, now that I understand my mistake in the recursive, my questions are mostly about the outside of the loop and recursive function and more about what you have setup around them :
I was first confused by the structure of your code and then I realised after reading it that 50% of it is the actual function doing the job and 50% is structuring the reading or the display. You use it the same in both logics. New idea for me, interesting to debug.
I do not know why you call the Console with a static modifier, I remember a lesson about this in sololearn, i'll go look for it. I guess it makes you able to call the (Write) with no arguments ?
Also the ForEach call is new, I see the logic but don't know what it is yet. I guess it is a LINQ fonction and will go see the implentation.
Finally, using the Enumerable class to create the array ... yep ... I did it by hand it was not necessary ! :D
Thanks again for all this, It was precious,
All the best
Pierre