Napisal sem skripto (program ukazne vrstice) v Pythonu, ki ti pri podani vsoti ter številu seštevancev te vsote izpiše vse kombinacije seštevancev. Opcijsko skripta sprejme tudi niz seštevancev, ki so že znani in potem izpiše vse kombinacije preostalih seštevancev.
#!/usr/bin/env python # coding: utf-8 import argparse parser = argparse.ArgumentParser(description='Outputs all combinations of N numbers that yields given S sum. You can also specify already known number(s) for the sum and it will output combinations of all remaining numbers.') parser.add_argument("N", help="number of fields", type=int) parser.add_argument("S", help="sum of fields", type=int) parser.add_argument("-n", "--numbers", help="all known numbers for the sum (without space)", type=str) args = parser.parse_args() n, r, k, res = args.N, args.S, args.numbers or '', set() for s in xrange(int('1'*n), int('9'*n) + 1): s = str(s) if len(set(s)) == len(s) and '0' not in s and sum(map(int, s)) == r: res.add(''.join(sorted(s))) for i in sorted(res): j = set(k) if j.issubset(i): print ' '.join(set(i).difference(j))
Primer 1: Če je vsota 10, število seštevancev pa je 3, potem skripta vrne izpis
1 2 7 1 3 6 1 5 4 3 2 5
Primer 2: Če pri primeru 1 vemo, da je en seštevanec število 2, potem dobimo izpis
1 7 3 5
Stopnja zahtevnosti uganke se zmanjša za stopnjo ali dve. 🙂