1

I am trying to translate a Python generator into a function in C. If it's worth mentioning, I'm trying to write a program that partitions an integer n, into l distinct parts. I believe that I've got most of it working, I do not kow how to implement yield. This is the generator

def ruleAscLen(n, l):
    a = [0 for i in range(n + 1)]
    k = 1
    a[0] = 0
    a[1] = n
    while k != 0:
        x = a[k - 1] + 1
        y = a[k] - 1
        k -= 1
        while x <= y and k < l - 1:
            a[k] = x
            y -= x
            k += 1
        a[k] = x + y
        yield a[:k + 1]

and this is what I have so far

void ruleAscLen(int n, int l)
{
    int a[n+1];
    int k = 1;
    a[0] = 0;
    a[1] = n;
    
    while(k != 0)
    {
        int x = a[k - 1]  + 1;
        int y = a[k] - 1;
        k -= 1;
    
        while(x <= y && k < l-1)
        {
            a[k] = x;
            y -= x;
            k++;
        }
        a[k] = x + y;
    }
}
murage kibicho
  • 546
  • 5
  • 14

0 Answers0