Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two Crystal balls failing for a custom test #61

Open
ppalmeida opened this issue Aug 17, 2023 · 2 comments
Open

Two Crystal balls failing for a custom test #61

ppalmeida opened this issue Aug 17, 2023 · 2 comments

Comments

@ppalmeida
Copy link

I wrote a manual test to investigate this algo better since I found it very interesting.

This test fails and returns -1

test("custom test", () => {
    const data = [
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        true,
        true,
        true,
    ];
    expect(two_crystal_balls(data)).toEqual(9);
});

The way I've found to make it pass was considering the extra/last step like:

// Note the `j <= jumpAmount` instead of ` j < jumpAmount`
for (let j = 0; j <= jumpAmount && i < breaks.length; ++j, ++i) {
    if (breaks[i]) {
        return i;
    }
}

Keeping that <= condition still passes the other default tests from Prime. Removing it will break my test case. Am I right or am I missing something?

@ppalmeida
Copy link
Author

Of course, this is the algo (exactly what it is in Primeagen video)

export default function two_crystal_balls(breaks: boolean[]): number {
    const jumpAmount = Math.floor(Math.sqrt(breaks.length));
    let i = jumpAmount;
    for (; i < breaks.length; i += jumpAmount) {
        if (breaks[i]) {
            break;
        }
    }

    i -= jumpAmount;

    for (let j = 0; j <= jumpAmount && i < breaks.length; ++j, ++i) {
        if (breaks[i]) {
            return i;
        }
    }

    return -1;
}

@JonathanD01
Copy link

The condition should be j <= jmpAmount as seen on the screenshot.

Skjermbilde 2023-08-19 224550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants