2

I am following a tutorial on Dynamic Programming on youtube to understand more about Recursive functions, and I am stuck where a spread operator is used.

Code in JavaScript

const howSum = (targetSum, numbers) =>{
    if(targetSum === 0) return [];
    if(targetSum < 0) return null;

    for(let num of numbers){
           const remainder = targetSum - num;
           const remainderResult = howSum(remainder, numbers);
            if(remainderResult != null){
                 return [...remainderResult, num];
            } 
    }
    return null;

};

This is the code in C# where I'm trying to replicate the function

class HowSumSlow {

    static dynamic HowSum(int targetSum, int[] numbers)
    {
            
        if (targetSum == 0) return numbers;
        if (targetSum < 0) return null;

        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null) { 
                //Code here//
            }
        }
        return null;
    }

    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3 };
        Console.WriteLine(HowSum(7, numbers));
    }
    
}

EDIT: Should I use a Dictionary and use a key? I don't understand how to work my way around this one.

  static Dictionary<int, int[]> spread = new Dictionary<int, int[]>();
            
        static dynamic HowSum(int targetSum, int[] numbers){
        ...
            if(spread.ContainsKey(remainderResult)){
                return spread[remainderResult];
        }
    }

EDIT:

class HowSumSlow {

    static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[] { };
        if (targetSum == 0) return empty;
        if (targetSum < 0) return null;

        
        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }
        return null;
    }
    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    }


}
2
  • [...remainderResult, num] is just taking the array remainderResult, and creating a new array by appending num. You can do the same in C# with e.g. remainderResult.Append(num).ToArray(). I'm not sure why you're using dynamic though... Commented Mar 2, 2022 at 11:53
  • Given that [...remainderResult, num] is just remainderResult.concat(num), I don't think you need to mimic spread, rather than just do the same operation. Which is immutably adding an item to array. Commented Mar 2, 2022 at 11:53

2 Answers 2

8

C# 12 introduced a spread operator that is similar to Javascript. It is still under preview but it will be in available in stable channel along with .Net 8

Their example is

int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[] single = [..row0, ..row1, ..row2];

See more here.

Sign up to request clarification or add additional context in comments.

Comments

2

There is no spread operator in c#, you could use the Append method in the System.Linq namespace. As this returns an IEnumerable<T> you'll also need to call ToArray() afterwards.

This line in JS

return [...remainderResult, num];

Could be the following in c#

return remainderResult.Append(num).ToArray();

Note that your method always returns int[] or null, so the return type should be int[] not dynamic!

13 Comments

It's giving me "System.Int32[]" in the terminal. Is there a way to print out the array?
@Ilian Iterate it and write out each element of the array
Or e.g. string.Join(", ", array)
Indeed, something like Console.WriteLine(String.Join(",",HowSum(7, numbers))); - see here: dotnetfiddle.net/xNBcDS
@canton7 so I tried doing Console.WriteLine(String.Join(",", HowSum(7, numbers))); but it's giving me 2,3,3,2,2 , instead of 2,2,3
|

Your Answer

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