in reply to Re^2: checking a set of numbers for consistency mod p
in thread checking a set of numbers for consistency mod p

> I think it's the same as my original algorithm. :)

Well your's is buggy! ;-)

This count on hashes looks weird and is obviously error prone.

If I were you I would have started with an test suite with automated cases. like [3, [0,0,1,0,0,1,0,0,X]] with X from 2..3 multiplied with all 2^9 combinations of ? multiplied with 9 rotations.

These are just 2*512*9 <= 9216 positive cases (some will duplicate)

IMHO all other arrays of length 9 and max entry 3, which can't be found in this set must be negative.

You could use a random function to generate them.

> I don't see how you'd find the correct answer for, say, 3, [0, 0, 0] without counting, using anything like this algorithm.

I said first pass search for a power >0 to identify the main column.

In the second pass do the validation.

All defined entries in the main column must be >0 but in all other columns they must be 0.

If there is no value >0 found in the first pass, you must find a column consisting of ? only in order to return true.

This 2 pass solution could be more efficient, but this depends on the distribution of cases, which only you know well.

Alternatively you could do a one pass check with just one array @seen initialized to undef to represent the columns.

Once an entry is 0 it must stay 0, once it's 1 it must stay 1 (1 for > 0) otherwise you bail out with fail.

You'll also need one scalar $main_col set to the index of the first >0 column. If there is ever another col-index with >0 you need to return a fail. (That's kind of a counter if you want)

If this loop ends with $main_col set, continue with that column.

If the loop ends with $main_col == undef', you need to a parse '@seen for at least one undef column. If it doesn't exist it's a fail otherwise a pass (a column which only consists of ? will always pass)

This one pass solution has several advantages

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery