Problem
Step 1: Given a list of numbers, generate all possible groupings (in order) given only the final number of desired groups.
For example, if my list of numbers were 1 to 4, and I wanted 2 final groups, the possibilities would be:
[1], [2,3,4]
[1,2], [3,4]
[1,2,3], [4]
Step 2: Perform arithmetic operations on those groups.
For example, if we chose addition, the final results would be:
1 + 234 = 235
12 + 34 = 46
123 + 4 = 127
Prior Research and Similar Problems
I've seen numerous examples on SO and elsewhere for problems involving variable amounts of groups, which utilize ranges and for loops, a la:
print [num_list[i:i+groups] for i in range(0,len(num_list),groups)]
But that's kind of the reverse of what I want - there, the lengths of the groups themselves are fixed save for the final one, and the number of groups oscillates.
This isn't homework, just an interesting problem I came across. Ideally, I'd need to be able to iterate over those separate sublists in order to perform the mathematical operations, so they'd need to be captured as well.
I have a feeling the solution will involve itertools, but I can't seem to figure out the combinatorics with grouping aspect.
Edit/Extension of Step 2
If I want to perform different operations on each of the partitions, can I still approach this the same way? Rather than specifiying just int.add, can I somehow perform yet another combination of all the main 4 operations? I.e.:
symbol_list = ['+','-','*','/']
for op in symbol_list:
#something
I'd wind up with possibilities of:
1 + 2 * 34
1 * 2 - 34
1 / 2 + 34
etc.
Order of operations can be ignored.
Final Solution
#!/usr/bin/env python
import sys
from itertools import combinations, chain, product
# fixed vars
num_list = range(_,_) # the initial list
groups = _ # number of groups
target = _ # any target desired
op_dict = {'+': int.__add__, '-': int.__sub__,
'*': int.__mul__, '/': int.__div__}
def op_iter_reduce(ops, values):
op_iter = lambda a, (i, b): op_dict[ops[i]](a, b)
return reduce(op_iter, enumerate(values[1:]), values[0])
def split_list(data, n):
for splits in combinations(range(1, len(data)), n-1):
result = []
prev = None
for split in chain(splits, [None]):
result.append(data[prev:split])
prev = split
yield result
def list_to_int(data):
result = 0
for h, v in enumerate(reversed(data)):
result += 10**h * v
return result
def group_and_map(data, num_groups):
template = ['']*(num_groups*2 - 1) + ['=', '']
for groups in split_list(data, num_groups):
ints = map(list_to_int, groups)
template[:-2:2] = map(str, ints)
for ops in product('+-*/', repeat=num_groups-1):
template[1:-2:2] = ops
template[-1] = str(op_iter_reduce(ops, ints))
if op_iter_reduce(ops, ints) == target:
print ' '.join(template)
group_and_map(num_list, groups)
Step 1:
I worked on all the possible combinations of the indexes:
Example:
Step 2:
First we have to transform the group of digits in numbers:
And then with
reduce
andoperator
perform the arithmetic:(Although this last sub-step is not really related to your problem)
Step 1: The easiest way I have found to think of splitting lists into groups like that is to try to get combinations of split locations. Here is an implementation:
Step 2: First you need to convert a list like
[[1], [2, 3, 4]]
, to one like[1, 234]
. You can do this with the following function:Now you can perform your operation on the resulting list using
reduce()
:Complete solution: Based on edit referencing need for different operators:
If you are on Python 2.6 or below and
itertools.combinations_with_replacement()
is not available, you can use the recipe linked here.Raymond Hettinger has written a recipe for finding all partitions of an iterable into
n
groups:yields