0

I'm looking for a script that could tell me all possible combinations

input   (9)
output  (8+1)(7+2)(6+3)(5+4)(9)

input   (10)
output  (9+1)(8+2)(7+3)(6+4)(5+5)(10)

I would still appreciate the ability to select as many digits as possible in the example it is 2 to 1 (even if only two is enough) something this way

there should be an exit at three

input   (10)
output  (7+1+2)(6+3+1)(5+4+1)(4+4+2)(3+6+2)(1+6+3)

here is at least part of the possible results

I'm a beginner and the only thing I can do is combine all the words but this is at another level thank you for your help

word = ""
word_array = []
for c in word:
    word_array.append(c)
import itertools
results = list(itertools.permutations(word_array))
o = ""
for r in results:
    o += "".join(r)+"\r\n"
    print("".join(r))
f = open(""+str(word)+".txt", "a")
f.write(o)
f.close()
exit()
Beast Who
  • 45
  • 7
  • It's not really at another level. Just create your array with the numbers up through your target (`list(range(1,N+1))`). You don't want `permutations`, you want `itertools.combinations`. You can call it once for all combos of 2, and once for all combos of 3. It's not really that hard -- give it a try. – Tim Roberts Feb 11 '22 at 01:11
  • @TimRoberts I'm new to programming and that's beyond me started here recently python could i ask for a code? – Beast Who Feb 11 '22 at 01:13
  • @TimRoberts unfortunately I can't advise you, it's too complicated for me – Beast Who Feb 11 '22 at 01:41
  • Does [this](https://stackoverflow.com/questions/18503096/python-integer-partitioning-with-given-k-partitions) (Python-specific) or [this language-agnostic](https://stackoverflow.com/questions/400794/generating-the-partitions-of-a-number) post answer your question? – kcsquared Feb 11 '22 at 02:09
  • @kcsquared yes partially yes they still need a limit on the number of numbers I enter sometimes it will be 2 and sometimes 3 sometimes more – Beast Who Feb 11 '22 at 02:17
  • The Python-based post specifically asks for the case where you specify the size of the partitions. Unless you mean something different by 'number of numbers', that appears to be what you're asking for. – kcsquared Feb 11 '22 at 02:19
  • `def part(n, k): def _part(n, k, pre): if n <= 0: return [] if k == 1: if n <= pre: return [[n]] return [] ret = [] for i in range(min(pre, n), 0, -1): ret += [[i] + sub for sub in _part(n-i, k-1, i)] return ret return _part(n, k, n) print(part(5, 2))` this thanx for solved – Beast Who Feb 11 '22 at 02:23

0 Answers0