Home > other >  How to translate generators in this algorithm from Python to C#?
How to translate generators in this algorithm from Python to C#?

Time:10-24

I am translating Python code to C#. Can anyone help please.

    def transpose_to_hilbert_integer(self, x:list) -> int: # x is a list of ints
    """Restore a hilbert integer (`h`) from its transpose (`x`).

    Args:
        x (list): transpose of h
            (n components with values between 0 and 2**p-1)

    Returns:
        h (int): integer distance along hilbert curve
    """
    x_bit_str = [HilbertsCurve._binary_repr(x[i], self.p) for i in range(self.n)]
    print(x_bit_str)
    h = int(''.join([y[i] for i in range(self.p) for y in x_bit_str]), 2)
    return h

Translating to this:

public int Transpose_to_hilbert_integer(List<int> x)
    {
        List<string> x_bit_str = new List<string>();
        for (int i = 0; i < this.n; i  )
        {
            x_bit_str.Add(HilbertsCurve._binary_repr(x[i], this.p));
        }

// second part of the code. Make h.
        return h;
    }

Stuck of the double list comprehention

CodePudding user response:

I did it, I used a normal nested for loop instead in C#. Double nested. And = concatenation instead of .join() to an empty string.

  • Related