Skip to content

Sunday Times Teaser 3024 – Triple Jump

by Bill Kinally

Published Sunday September 06 2020 (link)

From a set of playing cards, Tessa took 24 cards consisting of three each of the aces, twos, threes, and so on up to the eights. She placed the cards face up in single row and decided to arrange them such that the three twos were each separated by two cards, the threes were separated by three cards and so forth up to and including the eights, duly separated by eight cards. The three aces were numbered with a one and were each separated by nine cards. Counting from the left, the seventh card in the row was a seven.

In left to right order, what were the numbers on the first six cards?

15 Comments Leave one →
  1. Brian Gladman permalink

    • John Z permalink

      Once again, you’ve come up with a very elegant and instructive solution
      but the ‘if’ statement:

      is unnecessary because the range in the previous ‘for’ statement

      prevents this from ever being false:

      the maximum of k is 21-2*i Substituting this in the expression of the ‘if’ statement:

      21 – 2*i + 2*i + 2 < 24


      23 < 24

      which is always true (and also true for values of k less than the maximum).

      • Brian Gladman permalink

        Thanks John,

        I added the 2 * (11 – i) range to my original solution later and didn’t notice that
        the validity check was no longer needed. I’ll update it.

  2. Frits permalink

    • Brian Gladman permalink


      Thank you for posting a very interesting solution and one that is also very fast (it runs on my system in 350\(\mu\)s). I hope you won’t mind if I make a few comments.

      First, shouldn’t the comment on line 36 refer to card ‘positions’ rather than card ‘values’? Second, the construction on lines 49 and 50 is truly horrible! Although I encourage a two space indent on this site to save horizontal space, I otherwise encourage people who post here to stay close to the Python PEP8 style guide in order to give the site a consistent ‘look and feel’. And the construction on lines 49/50 is listed in PEP8 as a ‘definitely not’! In any event, as I’m sure you know, it is an easy Python one liner using the ‘A if C else B’ construct.

      On a more interesting note, Jim Randell had a specific reason for using middle card positions but once you hard code the index ranges this no longer gives you any advantage over the use of the lowest card positions and actually produces less clarity in the resulting code. Here, for example, is my version of your approach (which runs at the same speed on my system):

  3. John Z permalink

    • Brian Gladman permalink

      Hi John

      Thank you for posting your solution.

      I hope you don’t mind this request, but I would be most grateful if you could use Python’s style guide when formatting your code as this makes it easier to understand posted code and also helps to maintain a consistent ‘look and feel’ for this site as a whole.

      In the main, the guide (known in Python jargon as ‘PEP8’) encourages spaces around binary operators, equal signs, etc. and generally discourages statements on the same line after ‘:’s (it accepts very short statements here such as ‘return’ or ‘continue’ but I prefer to avoid these exceptions anyway since they can visually obscure program structure).

      The guide, which is available here, is well worth a read.

  4. Frits permalink

    Hi Brian,

    I like your “lvd” determination using enumerate. A space after “v:” would have improved my immediate understanding.

    I am not used coding conditional logic at the right hand side of an assignment. I knew it but forgot it. As line 49/50 is probably executed only once I coded it the old fashioned / lazy way without using too many lines. I have read the PEP8 guide but find the “definitely not” a bit harsh. It doesn’t look very ugly to me.

    I have made a different version of the first card variant.
    It uses hardcoded values for the possible first card positions.

    May I ask how you test (or debug) Python code?
    I use a normal text editor for the code and a DOS window for testing the code.

    • Brian Gladman permalink

      Hi Frits,

      I use two development environments depending on what sort of development I am doing. For most of my fun stuff (like that here) I use the Wing IDE. For my professional Python (and C/C++) code I use Microsoft Visual Studio.

      For timing Python code that runs in 100 milliseconds or more I typically use the Python profile program but for fast code I use a small timer class:

      and then embed the code to be timed in a with clause:

      I use ‘timed’ above as a decorator to time subroutines.

      This produces pretty consistent timing except if a timing run catches the garbage collector (which is obvious and hence easily avoided by doing a few repeat timings).. I don’t use total run times as I don’t want to include interpreter load times etc.

      • Frits permalink


        I will have a look at Wing IDE.

        Timer() runs fine with CPython 3.8.5 but not (yet) with PyPy (Python 3.6.9) as perf_counter_ns has only been introduced with version 3.7.

        • Brian Gladman permalink

          For my fun stuff I always use the latest Python version (soon to be 3.9) and I don’t use PyPy because they don’t offer it as a 64-bit application on Windows (and I don’t have the time to port it).

  5. Erling Torkildsen permalink

    • Frits permalink

      Changing the loop order from (b, c, d, e, f, g, h, a) to (g, a, h, f, e, d, c, b) reduces the elapsed time by a factor 10 on my computer.

  6. Erling Torkildsen permalink

    Thanks Frits, that is a significantly better result when it comes to speed. The loop for g is in fact no loop at all. It came by because at first I missed out that the seven had a fixed position. When I discovered the mistake (it gave multiple solutions) I fixed it the cheapest way possible within the existing design. Yet another weakness with my program, the way it stands now, is that it will only print one solution (the last) even if there were more than one.
    That is fixed by moving the last part of the program back into the nested loop and ensure that Y is emptied before examining each non-overlapping set of positions.

  7. Erling Torkildsen permalink

    An early version of my program listed above, produced four solutions when card seven’s position was not restricted. With some small changes I just tried to get the program to reproduce those four solutions, but it only managed to find two. So it was obviously not working the way it was supposed to. As I did not find the flaw, I have made a simpler version of it that seems to do the job. I have also reversed the order of the loops that Frits made me aware of.

Leave a Reply

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

Subscribe to this comment feed via RSS