Skip to content

Partition Unique

Jim Randell has developed a neat generic subroutine called filter_unique that can assist teasers of the form “if you knew X, then you could work out what Y is”.   He has made this and other useful routines for solving teasers available in his enigma.py library, which is also available here.   I call my version of Jim’s subroutine partition_unique because ‘filter’ implies for me that the output returns only a part of the input whereas ‘partition’ implies that the output divides the input into parts.

Update

My version of partition unique was recently updated after Frits took an interest in it and suggested that its performance could be significantly improved in situations where one of the functions involved is costly.

Here is the new version:

It is available here (with a further change since the ‘g_costly’ parameter is unnecessary). Here is a test program that illustrates the reason for the update:

There are two functions in my number theory library that can test for primes: is_prime(), which is fast, and factor(), which is slow. The test program above uses 100,000 random numbers to show that these two ways of testing for primes give the same results. It does this twice, one using the usual approach and one using the feature of the new version to deal with costly second functions, in our case the prime test “len(factor(n)) == 1”.

The previous version runs this test in 1.822 seconds, compared with 390 milliseconds for the new version.

An Example of Use

Here is a teaser that illustrates the use of partition_unique:

—————————————————————————
Sunday Times Teaser 2857 by Danny Roth

Published June 25 2017

Significant Errors

Last year George and Martha were going to visit one of their daughters. Her
house number was a two-figure number but unfortunately Martha wrote the
number incorrectly by making the first digit less than it should be. When
George discovered the error he commented that the incorrect number was a
whole number percentage of the correct one. If you knew that percentage
then you should be able to work out their daughter’s house number.

Then the daughter moved to a different two-figure house number, Martha
again wrote the number incorrectly by making the first digit less than it
should be, and again the incorrect number was a whole number percentage of
the correct one. If you knew that percentage then you should be able to
work out their daughter’s new house number.

What was the daughter’s original house number, and what is the new one?
—————————————————————————

which can be solved as follows: