Home | Articles | CV (pdf | short)
<2008-02-02> by Lorenzo

IQ test

Qualche giorno fa, mi sono imbattuto in questo test di intelligenza, proposto da ECM. Riporto il testo, per semplicita':

Using each of the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 only once, form a nine digit number A that can satisfy the following criteria:

What is A?

Show how you produced B, C, D, E, F, G & H.

Dopo un po', mi sono stufato, e ho prodotto questo.

def all_perms(str):
    if len(str) <=1:
        yield str
    else:
        for perm in all_perms(str[1:]):
            for i in range(len(perm)+1):
                yield perm[:i] + str[0:1] + perm[i:]

def list2num(p):
    y = 0
    d = len(p)
    for ip,pp in enumerate(p):
        y += 10**(d-ip-1) * pp
    return y

def check(p):
    for i in xrange(1,len(p)-1):
        if list2num(p[:-i]) % (9-i) != 0:
            return False
    return True

for p in all_perms(range(1,10)):
    if check(p):
        print p
        break

Run it in python, of course!