Sunday Times Teaser 2520
by Angela Newing
Published: 9 January 2011 (link)
I have a brand-new 11-digit phone number. The first and last digits are zero and the middle section uses each of the digits 1 to 9 once. If you called the number by pressing the buttons on the standard keypad of a modern phone, you would find no two consecutive digits of the number in the same row or column. Looking at the fifth and sixth digits, I see one is double the other. The second and third digits differ by two, the seventh is lower than the sixth and the 10th is odd, and one more than the eighth.
What is my number?
One Comment
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334from itertools import permutations# the digits in rows and columns on a telephone keypadrows = ((1, 2, 3), (4, 5, 6), (7, 8, 9))cols = ((1, 4, 7), (2, 5, 8, 0), (3, 6, 9))for p in permutations(range(1, 10), 9):t = (0, 0) + p + (0,)# one of the fifth and sixth digits is double the otherif not (t[5] == 2 * t[6] or t[6] == 2 * t[5]):continue# the difference between the seccond and third digits is twoif not abs(t[2] - t[3]) == 2:continue# the seventh digit is lower than the sixthif not t[7] < t[6]:continue# the tenth digit is odd and 1 more than the eighthif not (t[10] % 2 and t[10] == t[8] + 1):continue# on a standard telephone keypad no two consecutve# digits are in the same row or column.for i, j in zip(t[1:], t[2:]):if any(i in rc and j in rc for rc in rows + cols):breakelse:print('The number is {}'.format(''.join(str(x) for x in t[1:])))