Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help me finish this Python 3.x self-challenge

This is not homework.

I saw this article praising Linq library and how great it is for doing combinatorics stuff, and I thought to myself: Python can do it in a more readable fashion.

After half hour of dabbing with Python I failed. Please finish where I left off. Also, do it in the most Pythonic and efficient way possible please.

from itertools import permutations
from operator import mul
from functools import reduce
glob_lst = []
def divisible(n): return (sum(j*10^i for i,j in enumerate(reversed(glob_lst))) % n == 0)
oneToNine = list(range(1, 10))
twoToNine = oneToNine[1:]
for perm in permutations(oneToNine, 9):
    for n in twoToNine:
        glob_lst = perm[1:n]
        #print(glob_lst)
        if not divisible(n):
            continue
    else:
        # Is invoked if the loop succeeds
        # So, we found the number
        print(perm)

Thanks!

like image 866
Hamish Grubijan Avatar asked Apr 15 '10 22:04

Hamish Grubijan


4 Answers

Here's a short solution, using itertools.permutations:

from itertools import permutations

def is_solution(seq):
    return all(int(seq[:i]) % i == 0 for i in range(2, 9))

for p in permutations('123456789'):
    seq = ''.join(p)
    if is_solution(seq):
        print(seq)

I've deliberately omitted the divisibility checks by 1 and by 9, since they'll always be satisfied.

like image 131
Mark Dickinson Avatar answered Nov 01 '22 23:11

Mark Dickinson


Here's my solution. I like all things bottom-up ;-). On my machine it runs about 580 times faster (3.1 msecs vs. 1.8 secs) than Marks:

def generate(digits, remaining=set('123456789').difference):
    return (n + m
        for n in generate(digits - 1)
            for m in remaining(n)
                if int(n + m) % digits == 0) if digits > 0 else ['']

for each in generate(9):
    print(int(each))

EDIT: Also, this works, and twice as fast (1.6 msecs):

from functools import reduce

def generate():
    def digits(x):
        while x:
            x, y = divmod(x, 10)
            yield y
    remaining = set(range(1, 10)).difference
    def gen(numbers, decimal_place):
        for n in numbers:
            for m in remaining(digits(n)):
                number = 10 * n + m
                if number % decimal_place == 0:
                    yield number
    return reduce(gen, range(2, 10), remaining())

for each in generate():
    print(int(each))
like image 34
pillmuncher Avatar answered Nov 02 '22 00:11

pillmuncher


Here's my solution (not as elegant as Mark's, but it still works):

from itertools import permutations

for perm in permutations('123456789'):
    isgood = 1
    for i in xrange(9):
        if(int(''.join(perm[:9-i])) % (9-i)):
            isgood = 0
            break
    if isgood:
        print ''.join(perm)
like image 2
Justin Peel Avatar answered Nov 02 '22 01:11

Justin Peel


this is my solution, it is very similar to Marks, but it runs about twice as fast

from itertools import permutations

def is_solution(seq):
    if seq[-1]=='9':
        for i in range(8,1,-1):
            n = -(9-i)
            if eval(seq[:n]+'%'+str(i))==0:
                continue
            else:return False
        return True
    else:return False
for p in permutations('123456789'):
    seq = ''.join(p)
    if is_solution(seq):
        print(seq)
like image 1
gtmanfred Avatar answered Nov 02 '22 01:11

gtmanfred