[OT] Algorithm question

MysticZach via Digitalmars-d digitalmars-d at puremagic.com
Mon May 1 12:27:59 PDT 2017


On Monday, 1 May 2017 at 16:56:58 UTC, MysticZach wrote:
>    // choose a random partition proportionally
>    auto j = uniform(da.length - 1);
>    if (j < i) {
>       // the lower partition
>       int a = randomlySatisfyImpl(da[0..i], j);
>       if (a != -1) return a;
>       else return randomlySatisfyImpl(da[i+1 .. da.length], j - 
> (i + 1));
>    }
>    else {
>       // higher partition, investigate in reverse order
>       int a = randomlySatisfyImpl(da[i+1 .. da.length], j - (i 
> + 1));
>       if (a != -1) return i +1 + a;
>       else return i + 1 + randomlySatisfyImpl(da[0..i], j);

The line above has a bug. Replace it with:

        else {
           a = randomlySatisfyImpl(da[0..i], j);
           return (a == -1) ? -1 : i + 1 + a;
        }

>    }
> }

But the idea's the same. Hopefully it's clear.



More information about the Digitalmars-d mailing list