A senior programmer like me knows that primality-based problems like the one posed in your link are easily gamed.
Testing for small prime factors is easy - brute force is your friend. Testing for large prime factors requires more effort. So the first trick is to figure out the bounds to the problem. Is it int32? Then brute-force it. Is it int64, where you might have a value like the Mersenne prime 2^61-1? Perhaps it's time to pull out a math reference. Is it longer, like an unbounded Python int? Definitely switch to something like the GNU Multiple Precision Arithmetic Library.
In this case, the maximum value is 1,000, which means we can enumerate all distinct prime values in that range, and test for its presence in each input value, one one-by-one:
That worked without testing, though I felt better after I ran the test suite, which found no errors. Here's the test suite:
import unittest
class TestExamples(unittest.TestCase):
def test_example_1(self):
self.assertEqual(distinctPrimeFactors([2,4,3,7,10,6]), 4)
def test_example_2(self):
self.assertEqual(distinctPrimeFactors([2,4,8,16]), 1)
def test_2_is_valid(self):
self.assertEqual(distinctPrimeFactors([2]), 1)
def test_1000_is_valid(self):
self.assertEqual(distinctPrimeFactors([1_000]), 2) # (2*5)**3
def test_10_000_values_is_valid(self):
values = _primes[:20] * (10_000 // 20)
assert len(values) == 10_000
self.assertEqual(distinctPrimeFactors(values), 20)
@unittest.skipUnless(__debug__, "can only test in debug mode")
class TestConstraints(unittest.TestCase):
def test_too_few(self):
with self.assertRaisesRegex(AssertionError, "size out of range"):
distinctPrimeFactors([])
def test_too_many(self):
with self.assertRaisesRegex(AssertionError, "size out of range"):
distinctPrimeFactors([2]*10_001)
def test_num_too_small(self):
with self.assertRaisesRegex(AssertionError, "num out of range"):
distinctPrimeFactors([1])
def test_num_too_large(self):
with self.assertRaisesRegex(AssertionError, "num out of range"):
distinctPrimeFactors([1_001])
if __name__ == "__main__":
unittest.main()
I had two typos in my test suite (an "=" for "==", and a ", 20))" instead of "), 20)"), and my original test_num_too_large() tested 10_001 instead of the boundary case of 1_001, so three mistakes in total.
If I had no internet access, I would compute that table thusly:
_primes = [2]
for value in range(3, 1000):
if all(value % p > 0 for p in _primes):
_primes.append(value)
Do let me know of any remaining mistakes.
What kind of senior programmers do you work with who can't handle something like this?
EDIT: For fun I wrote an implementation based on sympy's integer factorization:
from sympy.ntheory import factorint
def distinctPrimeFactors(nums: list[int]) -> int:
distinct_factors = set()
for num in nums:
distinct_factors.update(factorint(num))
return len(distinct_factors)
Here's a new test case, which takes about 17 seconds to run:
Testing for small prime factors is easy - brute force is your friend. Testing for large prime factors requires more effort. So the first trick is to figure out the bounds to the problem. Is it int32? Then brute-force it. Is it int64, where you might have a value like the Mersenne prime 2^61-1? Perhaps it's time to pull out a math reference. Is it longer, like an unbounded Python int? Definitely switch to something like the GNU Multiple Precision Arithmetic Library.
In this case, the maximum value is 1,000, which means we can enumerate all distinct prime values in that range, and test for its presence in each input value, one one-by-one:
That worked without testing, though I felt better after I ran the test suite, which found no errors. Here's the test suite: I had two typos in my test suite (an "=" for "==", and a ", 20))" instead of "), 20)"), and my original test_num_too_large() tested 10_001 instead of the boundary case of 1_001, so three mistakes in total.If I had no internet access, I would compute that table thusly:
Do let me know of any remaining mistakes.What kind of senior programmers do you work with who can't handle something like this?
EDIT: For fun I wrote an implementation based on sympy's integer factorization:
Here's a new test case, which takes about 17 seconds to run: