# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1670: How mean?

From New Scientist #2836, 29th October 2011 [link]

I recently set a class the following puzzle: “Write down four positive whole numbers less than 100 and then write down their average: choose the original numbers so that overall the five numbers written down use each of the digits 1 to 9 exactly once.”

Three Scandinavian lads in the class were successful. Hola came up with 9, 15, 36 and 48, with average 27. Frack was more cunning – his average was not a whole number – it involved a fraction in its simplest form, like 27¾. Dessie was devious, too, and his average, which was lower than Frack’s, was not a whole number – it involved decimals, like 16.25.

What averages did Frack and Dessie write?

[enigma1670]

### One response to “Enigma 1670: How mean?”

1. Jim Randell 2 December 2011 at 3:20 pm

Slightly tricky, as we have to deal with both fractional and decimal representations.

This solution is not very quick: 14s (3.8s using PyPy).

from collections import defaultdict

F = set()
D = set()

def valid(m, *l):
d = defaultdict(int)
for i in l:
for j in list(str(i)):
if j in ('.', '/'): continue
if j == '0': return 0
if d[j]: return 0
d[j] = 1
if len(l) > 4 and len(d) == 9:
if l[5].find('/') > -1:
else:
print(*l)
return len(d)

for a in range(1, 100):
for b in range(a+1, 100):
if valid(a, b) == 0: continue
for c in range(b+1, 100):
if valid(a, b, c) == 0: continue
for d in range(c+1, 100):
if valid(a, b, c, d) == 0: continue
s = sum((a, b, c, d))
m = float(s) / 4
r = s % 4
if r == 0:
pass
elif r == 1:
valid(m, a, b, c, d, int(m), '1/4')
valid(m, a, b, c, d, int(m), '.25')
elif r == 2:
valid(m, a, b, c, d, int(m), '1/2')
valid(m, a, b, c, d, int(m), '.5')
elif r == 3:
valid(m, a, b, c, d, int(m), '3/4')
valid(m, a, b, c, d, int(m), '.75')

for f in F:
for d in D:
if not(d < f): continue
print("F={f} D={d}".format(f=f, d=d))

Solution: Frack’s average is 25 3/4. Dessie’s average is 24.75.