Skip to content

Sunday Times Teaser 3137 – Common Names

by BRG on November 5, 2022

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

6 Comments Leave one →
  1. Brian Gladman permalink

    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!

    • John Z permalink

      Brilliant second version!

    • John Z permalink

      line 35 of your code:

      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.

      • Brian Gladman permalink

        Python dictionaries preserve the insertion order for Python 3.7 and
        later versions. So the length of the for loop on line 39 could be
        shortened as you suggest.

  2. John Z permalink

    A different approach. Didn’t bother with printing names with initial capital letter.

  3. John Z permalink

    Version 2: same concept, tighter code

Leave a comment to Brian Gladman Cancel reply

Note: HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS