Sunday Times Teaser 2593 – Spell It Out
by Victor Bryant
Published: 3 June 2012 (link)
I have written down a very large number (but with fewer than twenty- five digits). If you spell out each of its digits as a word, then for each digit its last letter is the same as the first letter of the next digit (the last letter of the last digit being the same as the first letter of the first digit). One example of such a number would be 83821.
Neither my number nor the sum of its digits is a palindromic number (but in fact the original number is one more than a palindrome).
What is my number?
One Comment
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829303132333435from collections import defaultdict# map digits to their word formsd_word = { 0:'nought', 1:'one', 2:'two', 3:'three', 4:'four',5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine' }# produce a map from digits to the sets of possible next digitsdx = defaultdict(set)for first in d_word:for second in d_word:if d_word[first][-1] == d_word[second][0]:dx[first].update([second])# create a sequence meeting the teaser constraints (dl is the# digit sequence, nbr is the equivalent number)def place(dl, nbr, dx):len_dl = len(dl)if len_dl < 25:# check that first letter of first digit matches the last# letter of the last digitif len_dl > 1 and d_word[dl[0]][0] == d_word[dl[-1]][-1]:# check if the number is one above a palindromes1, s2 = str(nbr - 1), str(sum(dl))if s1 == s1[::-1] and not s2 == s2[::-1]:yield nbr# place the next digitfor i in dx[dl[-1]]:yield from place(dl + [i], 10 * nbr + i, dx)# try each digit in the first positionfor i in dx:for r in place([i], i, dx):print('The number is {}.'.format(r))