Teaser 2016: T for two
From The Sunday Times, 6th May 2001 [link]
I have in mind a whole number less than 100. If I were to tell you the first letter in the spelling of that number, you would not be able to determine the number. But if I were to tell you both the first and last letters in the spelling of the number, then you would be able to determine it. Now, If I were to tell you just the last letter in the spelling of the number, you would be able to determine it.
What is the number?
[teaser2016]
Jim Randell 9:33 am on 15 June 2023 Permalink |
I supposed the numbers were from 1 to 99 (although you can also use 0 if you like).
This Python program combines the [[ int2words() ]] and [[ filter_unique() ]] functions from the enigma.py library.
It runs in 58ms. (Internal runtime is 267µs).
Run: [ @replit ]
from enigma import (irange, int2words, filter_unique, intersect, join, printf) # numbers from 1 to 99 as words ns = dict((n, int2words(n)) for n in irange(1, 99)) # return a function to select letters at indices <js> from the spelling of a number select = lambda *js: (lambda k: join(ns[k][j] for j in js)) # the number is ambiguous by first letter ss1 = filter_unique(ns.keys(), f=select(0)).non_unique # but unique by first + last letter ss2 = filter_unique(ns.keys(), f=select(0, -1)).unique # from these it is unique by last letter ss = filter_unique(intersect([ss1, ss2]), f=select(-1)).unique # output solution printf("{ss}", ss=join(ss, sep=", "))Solution: The number is 98.
LikeLike
Frits 2:06 pm on 15 June 2023 Permalink |
Maintaining a dictionary.
from enigma import int2words # lowercase letters letters = ''.join(chr(ord('a') + i) for i in range(26)) # numbers from 1 to 99 as words nums = [int2words(n) for n in range(1, 100)] # the number is ambiguous by first letter d = {l: [x for x in nums if x[0] == l] for l in letters} d = {k: vs for k, vs in d.items() if len(vs) > 1} # but unique by first + last letter d = {l1 + l2: [x for x in nums if x[0] == l1 and x[-1] == l2 and x[0] in d] for l1 in letters for l2 in letters} d = {k: vs for k, vs in d.items() if len(vs) == 1} # from these it is unique by last letter d = {k[-1]: [vs2 for k2, vs2 in d.items() if k[-1] == k2[-1]] for k in d} d = {k: vs for k, vs in d.items() if len(vs) == 1} for k, v in d.items(): print("answer:", *v[0])LikeLike