I was working on part of a program in which I'm trying to input a list of numbers and return all groups of 3 numbers which sum to 0, without double or triple counting each number. Here's where I'm up to:
def threeSumZero2(array):
sums = []
apnd=[sorted([x,y,z]) for x in array for y in array for z in array if x+y+z==0]
for sets in apnd:
if sets not in sums:
sums.append(sets)
return sums
Is there any code I can put in the third line to make sure I don't return [0,0,0] as an answer.
This is my test list:
[-1,0,1,2,-1,4]
Thank you
*Edit: I should have clarified for repeated input values: the result expected for this test list is:
[[-1,-1,2],[-1,0,1]]
You want combinations without replacement, this is something offered by itertools. Your sums can then be made a set to remove the duplicates with regard to ordering.
from itertools import combinations
def threeSumZero2(array):
sums = set()
for comb in combinations(array, 3):
if sum(comb) == 0:
sums.add(tuple(sorted(comb)))
return sums
print(threeSumZero2([-1,0,1,2,-1,4]))
Output
{(-1, -1, 2), (-1, 0, 1)}
This solution can also be written more concisely using a set-comprehension.
def threeSumZero2(nums):
return {tuple(sorted(comb)) for comb in combinations(nums, 3) if sum(comb) == 0}
More efficient algorithm
Although, the above algorithm requires traversing all combinations of three items, which makes it O(n3).
A general strategy used for this kind of n-sum problem is to traverse the n-1 combinations and hash their sums, allowing to efficiently test them against the numbers in the list.
The algorithm complexity drops by one order of magnitude, making it O(n2)
from itertools import combinations
def threeSumZero2(nums, r=3):
two_sums = {}
for (i_x, x), (i_y, y) in combinations(enumerate(nums), r - 1):
two_sums.setdefault(x + y, []).append((i_x, i_y))
sums = set()
for i, n in enumerate(nums):
if -n in two_sums:
sums |= {tuple(sorted([nums[idx[0]], nums[idx[1]], n]))
for idx in two_sums[-n] if i not in idx}
return sums
print(threeSumZero2([-1,0,1,2,-1,4]))
Output
{(-1, -1, 2), (-1, 0, 1)}
You could do this with itertools (see Oliver's answer), but you can also achieve the result with three nested for-loops:
def threeSumZero2(lst):
groups = []
for i in range(len(lst)-2):
for j in range(i + 1, len(lst)-1):
for k in range(j + 1, len(lst)):
if lst[i] + lst[j] + lst[k] == 0:
groups.append((lst[i], lst[j], lst[k]))
return groups
and your test:
>>> threeSumZero2([-1, 0, 1, 2, -1, 4])
[(-1, 0, 1), (-1, 2, -1), (0, 1, -1)]
Oh and list != array!
Related
given a List in Python I want top create a dictionary that stores all possible two sums as keys and the corresponding indices as values, e.g.
list = [1,0,-1, 0]
Then I would to compute the dictionary {1:{0,1}, {0,3}, 0: {1,3},{0,2}, -1:{1,2}, {2,3}}.
I am having troubles finding out how to have a dictionary where one key corresponds to multiple values. If I use dict[sum]={i,j} I am always replacing the entries in my dictionary while instead I would like to add them.
Does anyone know if there exists a solution?
IIUC, use a dictionary with setdefault to add the results and itertools.combinations to generate the combinations of indices:
lst = [1,0,-1, 0]
from itertools import combinations
out = {}
for i,j in combinations(range(len(lst)), 2):
a = lst[i] # first value
b = lst[j] # second value
S = a+b # sum of values
# if the key is missing, add empty list
# append combination of indices as value
out.setdefault(S, []).append((i,j))
print(out)
Condensed variant:
out = {}
for i,j in combinations(range(len(lst)), 2):
out.setdefault(lst[i]+lst[j], []).append((i,j))
output:
{ 1: [(0, 1), (0, 3)],
0: [(0, 2), (1, 3)],
-1: [(1, 2), (2, 3)]}
Try this:
arr = [1, 0, -1, 0]
map = {}
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
s = arr[i] + arr[j]
if s not in map:
map[s] = []
map[s].append((i, j))
print(map)
(Re-posting, as I did not get any response to my previous post)
I am trying to write a Python code to generate weak integer compositions (partitions) of a number 'n' into 'k' parts but with a MINIMUM and MAXIMUM value constraint on each partition (see example given below). Also, the partitions have to be generated in lexicographic order. I have found some related posts but have not been able to implement it. Any help will be appreciated.
Example:
Possible integer partitions for n=5 in k=3 parts:
[5,0,0], [4,1,0], [4,0,1], [3,2,0], [3,1,1], [3,0,2], ..., [0,0,5]
After imposing the constraint that each integer in the partition has a MINIMUM value 0 and a MAXIMUM value 3, I should get:
[3,2,0], [3,1,1], [3,0,2], ...so on, only.
Related posts:
Elegant Python code for Integer Partitioning
Generate lexicographic series efficiently in Python
This kind of problem is most straightforward to solve with a recursive generator function. To generate partitions of n into k parts, we can select the first part v, and then recursively partition n - v into k - 1 parts.
You want earlier solutions to have larger numbers in the first position, so we'll choose v in descending order.
def constrained_partitions(n, k, min_elem, max_elem):
allowed = range(max_elem, min_elem-1, -1)
def helper(n, k, t):
if k == 0:
if n == 0:
yield t
elif k == 1:
if n in allowed:
yield t + (n,)
elif min_elem * k <= n <= max_elem * k:
for v in allowed:
yield from helper(n - v, k - 1, t + (v,))
return helper(n, k, ())
Example:
>>> for p in constrained_partitions(5, 3, 0, 3):
... print(p)
...
(3, 2, 0)
(3, 1, 1)
(3, 0, 2)
(2, 3, 0)
(2, 2, 1)
(2, 1, 2)
(2, 0, 3)
(1, 3, 1)
(1, 2, 2)
(1, 1, 3)
(0, 3, 2)
(0, 2, 3)
Given an array of positive integers, find the minimum number of subsets where:
The sum of each element in the subset does not exceed a value, k.
Each element from the array is only used once in any of the subsets
All values in the array must present in any of the subsets.
Basically, a 'filling' algorithm but need to minimize the containers and need to ensure everything gets filled. My current idea is to sort in descending order and start creating sets when the sum exceeds k, start the next one but not sure what is the better way.
EDIT:
Ex:
Inputs: arr = [1,2,3,4,5], k= 10
Output: [[1,4,5], [2,3]]
# Other solutions such as [[2,3,4],[1,5]] are also acceptable
# But the important thing is the number of sets returned is 2
In the output sets, all 1-5 are used and used only once in the sets. Hope this clears it up.
There may be a smarter way to just find the minimal number of sets, but here's some code which uses Knuth's Algorithm X to do the Exact Cover operation, and a function I wrote last year to generate subsets whose sums are less than a given value. My test code first finds a solution for the data given in the question, and then it finds a solution for a larger random list. It finds the solution for [1, 2, 3, 4, 5] with maximum sum 10 almost instantly, but it takes almost 20 seconds on my old 32 bit 2GHz machine to solve the larger problem.
This code just prints a single solution that is of the minimum size, but it wouldn't be hard to modify it to print all solutions that are of the minimum size.
""" Find the minimal number of subsets of a set of integers
which conform to these constraints:
The sum of each subset does not exceed a value, k.
Each element from the full set is only used once in any of the subsets.
All values from the full set must be present in some subset.
See https://stackoverflow.com/q/50066757/4014959
Uses Knuth's Algorithm X for the exact cover problem,
using dicts instead of doubly linked circular lists.
Written by Ali Assaf
From http://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html
and http://www.cs.mcgill.ca/~aassaf9/python/sudoku.txt
Written by PM 2Ring 2018.04.28
"""
from itertools import product
from random import seed, sample
from operator import itemgetter
#Algorithm X functions
def solve(X, Y, solution):
if X:
c = min(X, key=lambda c: len(X[c]))
for r in list(X[c]):
solution.append(r)
cols = select(X, Y, r)
yield from solve(X, Y, solution)
deselect(X, Y, r, cols)
solution.pop()
else:
yield list(solution)
def select(X, Y, r):
cols = []
for j in Y[r]:
for i in X[j]:
for k in Y[i]:
if k != j:
X[k].remove(i)
cols.append(X.pop(j))
return cols
def deselect(X, Y, r, cols):
for j in reversed(Y[r]):
X[j] = cols.pop()
for i in X[j]:
for k in Y[i]:
if k != j:
X[k].add(i)
#Invert subset collection
def exact_cover(X, Y):
newX = {j: set() for j in X}
for i, row in Y.items():
for j in row:
newX[j].add(i)
return newX
#----------------------------------------------------------------------
def subset_sums(seq, goal):
totkey = itemgetter(1)
# Store each subset as a (sequence, sum) tuple
subsets = [([], 0)]
for x in seq:
subgoal = goal - x
temp = []
for subseq, subtot in subsets:
if subtot <= subgoal:
temp.append((subseq + [x], subtot + x))
else:
break
subsets.extend(temp)
subsets.sort(key=totkey)
for subseq, _ in subsets:
yield tuple(subseq)
#----------------------------------------------------------------------
# Tests
nums = [1, 2, 3, 4, 5]
k = 10
print("Numbers:", nums, "k:", k)
Y = {u: u for u in subset_sums(nums, k)}
X = exact_cover(nums, Y)
minset = min(solve(X, Y, []), key=len)
print("Minimal:", minset, len(minset))
# Now test with a larger list of random data
seed(42)
hi = 20
k = 2 * hi
size = 10
nums = sorted(sample(range(1, hi+1), size))
print("\nNumbers:", nums, "k:", k)
Y = {u: u for u in subset_sums(nums, k)}
X = exact_cover(nums, Y)
minset = min(solve(X, Y, []), key=len)
print("Minimal:", minset, len(minset))
output
Numbers: [1, 2, 3, 4, 5] k: 10
Minimal: [(2, 3, 5), (1, 4)] 2
Numbers: [1, 2, 3, 4, 8, 9, 11, 12, 17, 18] k: 40
Minimal: [(1, 8, 9, 18), (4, 11, 17), (2, 3, 12)] 3
Assume, I have a NumPy-array of integers, as:
[34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]
I want to find the start and end indices of the array, where a value is more than x-times (say 5-times) repeated. So in the case above, it is the value 22 and 6. Start index of the repeated 22 is 3 and end-index is 8. Same for the repeatening 6.
Is there a special tool in Python that is helpful?
Otherwise, I would loop through the array index for index and compare the actual value with the previous.
Regards.
Using np.diff and the method given here by #WarrenWeckesser for finding runs of zeros in an array:
import numpy as np
def zero_runs(a): # from link
iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0]))
absdiff = np.abs(np.diff(iszero))
ranges = np.where(absdiff == 1)[0].reshape(-1, 2)
return ranges
a = [34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]
zero_runs(np.diff(a))
Out[87]:
array([[ 3, 8],
[15, 22]], dtype=int32)
This can then be filtered on the difference between the start & end of the run:
runs = zero_runs(np.diff(a))
runs[runs[:, 1]-runs[:, 0]>5] # runs of 7 or more, to illustrate filter
Out[96]: array([[15, 22]], dtype=int32)
Here is a solution using Python's native itertools.
Code
import itertools as it
def find_ranges(lst, n=2):
"""Return ranges for `n` or more repeated values."""
groups = ((k, tuple(g)) for k, g in it.groupby(enumerate(lst), lambda x: x[-1]))
repeated = (idx_g for k, idx_g in groups if len(idx_g) >=n)
return ((sub[0][0], sub[-1][0]) for sub in repeated)
lst = [34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]
list(find_ranges(lst, 5))
# [(3, 8), (15, 22)]
Tests
import nose.tools as nt
def test_ranges(f):
"""Verify list results identifying ranges."""
nt.eq_(list(f([])), [])
nt.eq_(list(f([0, 1,1,1,1,1,1, 2], 5)), [(1, 6)])
nt.eq_(list(f([1,1,1,1,1,1, 2,2, 1, 3, 1,1,1,1,1,1], 5)), [(0, 5), (10, 15)])
nt.eq_(list(f([1,1, 2, 1,1,1,1, 2, 1,1,1], 3)), [(3, 6), (8, 10)])
nt.eq_(list(f([1,1,1,1, 2, 1,1,1, 2, 1,1,1,1], 3)), [(0, 3), (5, 7), (9, 12)])
test_ranges(find_ranges)
This example captures (index, element) pairs in lst, and then groups them by element. Only repeated pairs are retained. Finally, first and last pairs are sliced, yielding (start, end) indices from each repeated group.
See also this post for finding ranges of indices using itertools.groupby.
There really isn't a great short-cut for this. You can do something like:
mult = 5
for elem in val_list:
target = [elem] * mult
found_at = val_list.index(target)
I leave the not-found exceptions and longer sequence detection to you.
If you're looking for value repeated n times in list L, you could do something like this:
def find_repeat(value, n, L):
look_for = [value for _ in range(n)]
for i in range(len(L)):
if L[i] == value and L[i:i+n] == look_for:
return i, i+n
Here is a relatively quick, errorless solution which also tells you how many copies were in the run. Some of this code was borrowed from KAL's solution.
# Return the start and (1-past-the-end) indices of the first instance of
# at least min_count copies of element value in container l
def find_repeat(value, min_count, l):
look_for = [value for _ in range(min_count)]
for i in range(len(l)):
count = 0
while l[i + count] == value:
count += 1
if count >= min_count:
return i, i + count
I had a similar requirement. This is what I came up with, using only comprehension lists:
A=[34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]
Find unique and return their indices
_, ind = np.unique(A,return_index=True)
np.unique sorts the array, sort the indices to get the indices in the original order
ind = np.sort(ind)
ind contains the indices of the first element in the repeating group, visible by non-consecutive indices
Their diff gives the number of elements in a group. Filtering using np.diff(ind)>5 shall give a boolean array with True at the starting indices of groups. The ind array contains the end indices of each group just after each True in the filtered list
Create a dict with the key as the repeating element and the values as a tuple of start and end indices of that group
rep_groups = dict((A[ind[i]], (ind[i], ind[i+1]-1)) for i,v in enumerate(np.diff(ind)>5) if v)
I am struggling about a recursion problem, which should not be too hard, but for some reason I don't come up with a solution.
I have an array of size "n" and I want to count up each element from 0 to n in a way that I get each possible combination.
n = 3
[0,0,0]
[0,0,1]
[0,1,0]
[1,0,0]
[... ]
[3,3,3]
Can anyone help?
If you have to code it up yourself, and have to use recursion:
def gen(n, l, prefix=()):
if l == 0:
print prefix
else:
for i in range(n):
gen(n, l - 1, prefix + (i,))
gen(4, 3)
No need for (explicit) recursion:
import itertools
for comb in itertools.product(range(4), repeat=3):
print comb
produces:
(0, 0, 0)
(0, 0, 1)
(0, 0, 2)
(0, 0, 3)
(0, 1, 0)
(0, 1, 1)
...
(3, 3, 2)
(3, 3, 3)
Here's one way to do it that makes the procedure very explicit:
def combinations(n, elements = None):
if elements == 0: return [[]]
if not elements: elements = n
result = []
for v in range(n + 1):
for subcombination in combinations(n, elements - 1):
result.append([v] + subcombination)
return result
There are more pythonic ways to do it that might have better performance, including comprehensions or generators, but it sounds like you're looking for an explicit implementation.