Python Code for C #

My ultimate goal is to convert the code below in python to C #, but I would like to do it myself by learning the python syntax. I understand that the code is recursive.

The code generates polynomials of degree n with k variables. More specifically, a list of metrics for each variable.

def multichoose(n,k):
    if k < 0 or n < 0: return "Error"
    if not k: return [[0]*n]
    if not n: return []
    if n == 1: return [[k]]
    return [[0]+val for val in multichoose(n-1,k)] + \
        [[val[0]+1]+val[1:] for val in multichoose(n,k-1)]

Here is the conversion that I have so far:

public double[] MultiChoose(int n, int k)
{
    if (k < 0 || n < 0)
    {
         throw new Exception();
    }

   if (k == 0)
   {
       return [[0]*n]; // I have no idea what the [[0]*n] syntax means
   }

   if (n == 0)
   {
       return new double[0]; // I think this is just an empty array
   }

   if (n == 1)
   {
       return new double[1] {k}; // I think this is just an empty array
   }

   //Part I don't understand
   return [[0]+val for val in MultiChoose(n-1,k)] + \
        [[val[0]+1]+val[1:] for val in MultiChoose(n,k-1)]
} 

My question is: how do I convert python code?

+5
source share
4 answers

I would use LINQ in C # to translate the code:

  • [] - an empty list.

    Enumerable.Empty<T>()
    
  • [x] is a list containing one item, x.

    Enumerable.Repeat(x, 1)
    
  • [[0]*n] - a list containing a list containing n copies of 0.

    Enumerable.Repeat(Enumerable.Repeat(0, n), 1)
    
  • [X for Y in Z] - this is an understanding of the list.

    from Y in Z select X
       - or -
    Z.Select(Y => X);
    
  • X + Y (where X and Y are lists) is the concatenation of the list.

    Enumerable.Concat(X, Y)
    

The MultiChoose signature will be:

 public IEnumerable<IEnumerable<double>> MultiChoose(int n, int k);
+3

[0] * n n 0 s. [] - . [[k]] - , k.

. :

[<new value> for <name> in <sequence>]
[<new value> for <name> in <sequence> if <condition>]

, , , .

+1

:

return "Error" . "".

Pythons if not k: if (k == 0), , "", , "" .. ( ).

Pythons foo = [for x in bar] - . :

foo = []
for x in bar:
   foo.append(x)
+1

Python . # 3.0 .NET Framework 3.5.

public static IEnumerable<IEnumerable<int>> MultiChoose(int n, int k)
{
    if (k < 0 || n < 0) throw new Exception();
    if (k == 0) return Enumerable.Repeat(Enumerable.Repeat(0, n), 1);
    if (n == 0) return Enumerable.Repeat(Enumerable.Empty<int>(), 0);
    if (n == 1) return Enumerable.Repeat(Enumerable.Repeat(k, 1), 1);

    return (from val in MultiChoose(n - 1, k) select new [] { 0 }.Concat(val))
        .Concat(from val in MultiChoose(n, k - 1) select new [] { val.First() + 1 }.Concat(val.Skip(1)));
}

Ruby

def multichoose(n,k)
  if k<0 || n<0: raise "Error" end
  if k==0: return [[0]*n] end
  if n==0: return [] end
  if n==1: return [[k]] end
  multichoose(n-1,k).map{|val| [0]+val} + \
    multichoose(n,k-1).map{|val| [val.first+1]+val[1..-1]}
end

:

>> multichoose(2,2)
=> [[0, 2], [1, 1], [2, 0]]
>> multichoose(3,2)
=> [[0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]]
>> multichoose(3,3)
=> [[0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0]]
+1

All Articles