### by Victor Bryant

#### Published Sunday November 06 2022 (link)

Eight friends met at a party; their ages in whole numbers of years were all different. They were Alan, Cary, James, Lucy, Nick, Ricky, Steve and Victor, with Lucy being the youngest. For each of them the square of their age was a three-figure number consisting of three different digits. Furthermore, for any two of them, the squares of their ages had at least one digit in common precisely when their names had at least one letter in common.

In alphabetical order of their names, what are the eight ages?

From → Uncategorized

Here is my first version:

Here is a simpler and faster version:

I spent sometime tracking down a subtle bug in these programs, thinking that the following two comprehensions are the same:

Here is an explanation of the second comprehension (credit MRAB):

The comparison operators can be chained, so:

a == b == c

is equivalent to:

(a == b) and (b == c)

except that the common term (‘b’ in this case) is evaluated only once.

‘in’ is one of those comparison operators, so:

srt(m, n) in c_np == srt(a, b) in c_ap

is equivalent to:

(srt(m, n) in c_np) and (c_np == srt(a, b)) and (srt(a, b) in c_ap)

except that the common terms (‘c_np’ and ‘srt(a, b)’) are evaluated only once.

Chaining makes most sense with multiple ‘==’ or a series of ‘<' and/or '<=' or a series of '>‘ and/or ‘>=’, as in ‘1 <= n <= 10'.

Coming to Python from C/C++ it is hard not to see ‘==’ as having lower priority than pretty well everything else!

Brilliant second version!

seems to assume that list(a2s) gives a sorted list in increasing order of keys of a2s or at least that it returns the keys in the order in which they were created. If a2s were a set and not a dictionary, this wouldn’t necessarily be true. Is the ‘first in first out’ rule valid for dictionaries? Writing this line as:

would eliminate any doubts on this score.
Line 39 can then be written:

as there must be at least 8 ages in the result.