All permutations of integers corresponded to specific sum - python

I wanted to generate all permutations from the list of integers [3,5,7,9] which resulted in a specific sum value 15. I implemented this, it's OK.
def add_next(seq, count, m):
s = sum(seq)
if s == m:
count += 1
print(seq)
elif s < m:
for i in [3,5,7,9]:
add_next(seq + [i], count, m)
else:
return count
add_next([], 0, 15)
Output:
[3, 3, 3, 3, 3]
[3, 3, 9]
[3, 5, 7]
[3, 7, 5]
[3, 9, 3]
[5, 3, 7]
[5, 5, 5]
[5, 7, 3]
[7, 3, 5]
[7, 5, 3]
[9, 3, 3]
The problem is how to re-write this function to return just the number of possible permutations as a function result? Since for huge lists and big sum values it's not reasonable to generate all string outputs. I don't fully understand how to pass values inside and outside recursive function.
I tried:
def add_next2(seq, count, m):
s = sum(seq)
if s == m:
count += 1
print(seq)
elif s < m:
for i in [3,5,7,9]:
count = add_next2(seq + [i], count, m)
else:
return count
add_next([], 0, 15)
but it retuns an error TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int'. So count is None. Why?
Another option is how to re-write this function to transform it to the generator and yield the output strings one after another?

If you're just counting successful recursive results, you don't need 'count' as a parameter. You can just return the successful results as 1 and unsuccessful as 0, letting them accumulate.
EDIT 2 A little more concise but still readable
def add_next(seq, m):
s = sum(seq)
count = 1 if s == m else 0
if s < m:
for i in [f for f in [3,5,7,9] if s + f <= m]:
count += add_next(seq + [i], m)
return count
print(add_next([], 15))
EDIT You can also filter your [3,5,7,9] list so that your for i in loop only deals with elements that have a possibility of success.
for i in [f for f in [3,5,7,9] if s + f <= m]:

Your recursive function doesn't return a value for s <= m. Return something from your function for those cases, otherwise None is returned instead.
Most likely you want to return count in all cases:
def add_next2(seq, count, m):
s = sum(seq)
if s == m:
count += 1
elif s < m:
for i in [3,5,7,9]:
count = add_next2(seq + [i], count, m)
return count
This then works:
>>> def add_next2(seq, count, m):
... s = sum(seq)
... if s == m:
... count += 1
... elif s < m:
... for i in [3,5,7,9]:
... count = add_next2(seq + [i], count, m)
... return count
...
>>> add_next2([], 0, 15)
11

Related

Python remove N consecutive duplicates from the list

Here's the problem. The input is a list of integers. If more than three adjacent numbers appear next to each other they should be dropped and the operation goes again. Kind of similar to the Iphone game, where player needs to pop lines of three or more balls of the same colors. The output should be the count of the balls that will be removed.
The algorithm is as follows. Starting with a sample list of say [3,3,4,4,4,4,3,2].
First iteration should remove the 4,4,4,4 - so the list would become [3,3,3,2], and the intermediary output of removed numbers will be 4.
Second iteration should remove 3,3,3 - so the final list would be [2] and final count of removed numbers - 7.
The first implementation for three consecutive items came from another stackoverflow thread - Remove triplets of adjacent numbers from the list
Here's the working function implementation for exactly 3 consecutive numbers:
def balls(l):
values = l
while len(values) >= 3:
print(values) #for demonstrative purposes of this question
for x in range(0,len(values)-2):
if values[x] == values[x+1] and values[x] == values[x+2]:
values = values[:x] + values[x+3:]
break
else:
break
print(values) #for demonstrative purposes of this question
return len(l) - len(values)
balls([3, 3, 4, 4, 4, 3, 4])
Output:
[3, 3, 4, 4, 4, 3, 4]
[3, 3, 3, 4]
[4]
6
How could I update the implementation to include the more general solution of removing 3+ consecutive numbers. I am thinking about tracking the start and end index of the consecutive duplicates, then subsetting the list. However, not sure how to implement that. Here are the tests that should work.
if __name__ == "__main__":
test1 = [3, 3, 4, 4, 4, 3, 4]
print(balls(test1))
#Output should be 6
test2 = [5, 5, 5, 5, 5, 5, 5, 5]
print(balls(test2))
#Output should be 8
test3 = [5, 7, 8, 3]
print(balls(test3))
#Output should be 0
def remove_consecutive(l, length):
amount = len(l)
count = 1
start = 0
current = l[0]
i = 1
while i < len(l):
if l[i] == current:
count += 1
else:
if count >= length:
for i in range(count):
l.pop(start)
start = 0
i = 0
current = l[0]
else:
start = i
current = l[i]
count = 1
i+=1
if count >= length:
for i in range(count):
l.pop(start)
return amount - len(l)
Wuff, i got it. My brain is kinda stinky lately so it took so long.
Here is my code, it works well. But I think there may be better ways to achieve higher efficiency.
def remove_consecutive(lst):
len_init = len(lst)
contain_tuplets = True
while contain_tuplets:
for i in range(len(lst)-2):
indices_to_pop = []
if lst[i]==lst[i+1]==lst[i+2]:
indices_to_pop.extend([i, i+1, i+2])
for j in range(i+3,len(lst)):
if lst[j] == lst[i]:
indices_to_pop.append(j)
else:
break
[lst.pop(i) for _ in indices_to_pop]
contain_tuplets = True
break
else:
contain_tuplets = False
count_removed_numbers = len_init - len(lst)
return count_removed_numbers, lst
test case1:
lst = [3,3,4,4,4,4,3,2]
remove_consecutive(lst)
output
(7, [2])
test case 2:
lst = [2, 2, 1, 1, 1, 2, 1]
remove_consecutive(lst)
output:
(6, [1])
def remove_consecutive(l, length):
amount = 0
count = 1
current = l[0]
for i in range(1, len(l)):
if l[i] == current:
count += 1
if count > length:
amount += 1
elif count == length:
amount += length
else:
current = l[i]
count = 1
return amount

Output error in this simple code for ArrayRotation?

Wrote a simple code for left array rotation, getting the same array without any Rotation done to it as the wrong output.
def leftRotate(arr, d, n):
while (d-1) > 0:
leftRotatebyOne(arr, n)
def leftRotatebyOne(arr, n):
temp = arr[0]
for i in range(n-1):
arr[i] = arr[i + 1]
arr[n - 1] = temp
def PrintArray(arr, size):
for i in range(size):
print("%d" % arr[i], end=" ")
arr = []
l = int(input("Enter the number of elements: "))
for i in range(0, l):
ele = int(input())
arr.append(ele)
d = int(input("Enter the number of rotations: "))
n = len(arr)
leftRotate(arr, d, n)
PrintArray(arr, n)
and here's an example of the output i've got,
Enter the number of elements: 3
1
2
3
Enter the number of rotations: 1
1 2 3
I expected an output of 2 3 1 after one rotation.
I would suggest using array slicing, then adding the slices together, to perform rotation.
def left_rotate(data, num):
return data[num:] + data[:num]
def right_rotate(data, num):
return data[-num:] + data[:-num]
For example
>>> a = [1,2,3,4,5,6,7]
>>> left_rotate(a, 2)
[3, 4, 5, 6, 7, 1, 2]
>>> right_rotate(a, 2)
[6, 7, 1, 2, 3, 4, 5]
Also note that collections.deque has this behavior available already
>>> from collections import deque
>>> d = deque([1,2,3,4,5,6,7])
>>> d.rotate(2)
>>> d
deque([6, 7, 1, 2, 3, 4, 5])
>>> d.rotate(-2)
>>> d
deque([1, 2, 3, 4, 5, 6, 7])
In the function leftRotate,
there is an error in while loop.
Replace
while (d-1) > 0:
leftRotatebyOne(arr, n)
with
while d > 0:
leftRotatebyOne(arr, n)
d -= 1
When d == 1, while (d-1) > 0: will not be executed any time. Also, you never decrement d. The easiest way to solve is by using a for _ in range(d) loop:
def leftRotate(arr, d, n):
for _ in range(d):
leftRotatebyOne(arr, n)
NOTE: Python has way better ways to do rotations than this. This code seems to be C more than Python. Passing the array length makes no sense in Python for example. And the rotation can be done all in one assignation.
def leftRotate(arr, d):
d %= len(arr)
for _ in range(d):
arr[-1], arr[:-1] = arr[0], arr[1:]
Cory Kramer's answer is even more pythonic. But it has a bug and a difference with your question's methods. The bug is that it doesn't work when the number of rotations requested are higher than the length of the list. The difference is that they are returning a new list instead of modifying it. These two issues could be addresed like this:
def left_rotate(data, num):
num %= len(data)
data[:] = data[num:] + data[:num]
def right_rotate(data, num):
num %= len(data)
data[:] = data[-num:] + data[:-num]

Find n integers in list that after multiplying equal to m

I need to print out n indexes of elements of list that after multiplying equal to some given integer. It's guaranteed that the combination exists in a list. For example, for the following input(number of elements in array, multiplication wanted number, number of elements in wanted sublist and given array):
7 60 4
30 1 1 3 10 6 4
I should get in any order
1 2 4 5
Because 1*1*10*6==60. If there are more than 1 solution I need to print any of them.
My solution works but pretty slow, how can I make it work faster?
from itertools import chain, combinations
arr = list(map(int, input().split()))
numbers = list(map(int, input().split()))
s = sorted(numbers)
def filtered_sublists(input_list, length):
return (
l for l in all_sublists(input_list)
if len(l) == length
)
def all_sublists(l):
return chain(*(combinations(l, i) for i in range(len(l) + 1)))
def multiply(arr):
result = 1
for x in arr:
result = result * x
return result
def get_indexes(data):
indexes = []
for i in range(len(data)):
if arr[1] == multiply(data[i]):
for el in data[i]:
if numbers.index(el) in indexes:
all_ind = [i for i, x in enumerate(numbers) if x == el]
for ind in all_ind:
if ind not in indexes:
indexes.append(ind)
break
else:
indexes.append(numbers.index(el))
break
return indexes
sublists = list(filtered_sublists(numbers, arr[2]))
print(*get_indexes(sublists))
The key is don't test every combination.
def combo(l, n=4, target=60, current_indices=[], current_mul=1):
if current_mul > target and target > 0:
return
elif len(current_indices) == n and current_mul == target:
yield current_indices
return
for i, val in enumerate(l):
if (not current_indices) or (i > current_indices[-1] and val * current_mul <= target):
yield from combo(l, n, target, current_indices + [i], val * current_mul)
l = [30,1,1,3,10,6,4]
for indices in combo(l, n=4, target=60):
print(*indices)
Prints:
1 2 4 5
More testcases:
l = [1,1,1,2,3,3,9]
for c, indices in combo(l, n=4, target=9):
print(*indices)
Prints:
0 1 2 6
0 1 4 5
0 2 4 5
1 2 4 5
We can use a memoized recursion for an O(n * k * num_factors), solution, where num_factors depends on how many factors of the target product we can create. The recurrence should be fairly clear from the code. (Zeros aren't handled but those should be pretty simple to add extra handling for.)
Pythonesque JavaScript code:
function f(A, prod, k, i=0, map={}){
if (i == A.length || k == 0)
return []
if (map[[prod, k]])
return map[[prod, k]]
if (prod == A[i] && k == 1)
return [i]
if (prod % A[i] == 0){
const factors = f(A, prod / A[i], k - 1, i + 1, map)
if (factors.length){
map[[prod, k]] = [i].concat(factors)
return map[[prod, k]]
}
}
return f(A, prod, k, i + 1, map)
}
var A = [30, 1, 1, 3, 10, 6, 4]
console.log(JSON.stringify(f(A, 60, 4)))
console.log(JSON.stringify(f(A, 60, 3)))
console.log(JSON.stringify(f(A, 60, 1)))
You could start from the target product and recursively divide by factors in the remaining list until you get down to 1 and after using the specified number of factors. This has the advantage of quickly eliminating whole branches of recursion under numbers that are not a factor of the target product.
Handling zero values in the list and a target product of zero requires a couple of special conditions at the start and while traversing factors.
For example:
def findFactors(product, count, factors, offset=0):
if product == 0: return sorted((factors.index(0)+i)%len(factors) for i in range(count))
if not count: return [] if product == 1 else None
if not factors: return None
for i,factor in enumerate(factors,1):
if factor == 0 or product%factor != 0: continue
subProd = findFactors(product//factor,count-1,factors[i:],i+offset)
if subProd is not None: return [i+offset-1]+subProd
r = findFactors(60, 4, [30,1,1,3,10,6,4])
print(r) # [1, 2, 4, 5]
r = findFactors(60, 4, [30,1,1,0,3,10,6,4])
print(r) # [1, 2, 5, 6]
r = findFactors(0, 4, [30,1,1,3,10,6,0,4])
print(r) # [0, 1, 6, 7]

Shortest subset for given sum and fastest solution in Python

This is a variant of "given sum problem" and I try to write a solution in Python which will solve it in O(log n) time. For a given natural number N (equal or larger than 1) find the shortest count of items p1..n which sum makes the N, and p items are a product of below iteration:
value of pi is either pi-1* 2 orpi-1+ 1
start from p1 which is exactly 1
so accordingly:
p2 is always 2, but p3 can be either 3 or 4
For the input N = 18, candidate sets are: [1, 2, 4, 5, 6], [1, 2, 3, 4, 8], [1, 2, 4, 5, 6], [1, 2, 3, 4, 8]
And the answer is 5.
This is code I wrote so far but it's slow and freezes on moderately "big" (N >= 1000) values:
possible = None
def solution(N):
global possible
possible = list()
tea(1, [1], N)
sizes = [len(p) for p in possible]
return min(sizes)
pass
def tea(n, l, target):
global possible
if (sum(l) > target):
return
elif (sum(l) == target):
possible.append(l)
i = n * 2
tea(i, l + [i], target)
i = n + 1
tea(i, l + [i], target)
print solution(18)
# should print 5
print solution(220)
# should print 11
print solution(221)
# no such solution? print -1
How to solve it in a more efficient way?
Fastest solutions are most crucial but a more pythonic code is appreciated as well.
Use breadth-first search to reduce wasted effort. The code below could be optimized more.
def solution(n):
q = [(1,)]
visited = set()
for seq in q:
s = sum(seq)
if s == n:
return seq
elif s > n:
continue
key = (seq[-1], s)
if key in visited:
continue
visited.add(key)
q.append(seq + (seq[-1] * 2,))
q.append(seq + (seq[-1] + 1,))
return None
You are looking for the shortest solution, so if you found a solution, there is no need to look for longer ones.
you can change the code so it will not look for solutions longer then what you have found this way:
(note the added if condition)
def tea(n, l, target):
global possible
if (sum(l) > target):
return
elif (sum(l) == target):
possible.append(l)
# we want to keep looking for new solutions only if l is shorter!
if possible and (len(l) >= max(len(i) for i in possible)):
return
i = n * 2
tea(i, l + [i], target)
i = n + 1
tea(i, l + [i], target)
in addition, seems like you want the function to return -1 when there is no solution, currently, your code will raise an error on such cases, I would change solution() funtion to this:
possible = []
def solution(N):
global possible
tea(1, [1], N)
sizes = [len(p) for p in possible] # you can use: size = map(len,possible) instead
if sizes:
return min(sizes)
return -1
and as for your "more pythonic code", I would write it this way:
def solution(N):
possibles =[]
tea(1, [1], N, possibles)
if not possibles:
return -1
else:
return min(map(len,possibles))
def tea(n, l, target, possibles): # maybe a better name then "tea"
if (sum(l) > target):
return
elif (sum(l) == target):
possibles.append(l)
return
if possibles and (len(l) >= max(len(i) for i in possibles)):
return
tea(n * 2, l + [n * 2], target, possibles)
tea(n + 1, l + [n + 1], target, possibles)

Python Integer Partitioning with given k partitions

I'm trying to find or develop Integer Partitioning code for Python.
FYI, Integer Partitioning is representing a given integer n as a sum of integers smaller than n. For example, an integer 5 can be expressed as 4 + 1 = 3 + 2 = 3 + 1 + 1 = 2 + 2 + 1 = 2 + 1 + 1 + 1 = 1 + 1 + 1 + 1 + 1
I've found a number of solutions for this. http://homepages.ed.ac.uk/jkellehe/partitions.php and http://code.activestate.com/recipes/218332-generator-for-integer-partitions/
However, what I really want is to restrict the number of partitions.
Say, # of partition k = 2, a program only need to show 5 = 4 + 1 = 3 + 2,
if k = 3, 5 = 3 + 1 + 1 = 2 + 2 + 1
I've written a generator solution
def partitionfunc(n,k,l=1):
'''n is the integer to partition, k is the length of partitions, l is the min partition element size'''
if k < 1:
raise StopIteration
if k == 1:
if n >= l:
yield (n,)
raise StopIteration
for i in range(l,n+1):
for result in partitionfunc(n-i,k-1,i):
yield (i,)+result
This generates all the partitions of n with length k with each one being in order of least to greatest.
Just a quick note: Via cProfile, it appears that using the generator method is much faster than using falsetru's direct method, using the test function lambda x,y: list(partitionfunc(x,y)). On a test run of n=50,k-5, my code ran in .019 seconds vs the 2.612 seconds of the direct method.
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)
Example:
>>> part(5, 1)
[[5]]
>>> part(5, 2)
[[4, 1], [3, 2]]
>>> part(5, 3)
[[3, 1, 1], [2, 2, 1]]
>>> part(5, 4)
[[2, 1, 1, 1]]
>>> part(5, 5)
[[1, 1, 1, 1, 1]]
>>> part(6, 3)
[[4, 1, 1], [3, 2, 1], [2, 2, 2]]
UPDATE
Using memoization:
def part(n, k):
def memoize(f):
cache = [[[None] * n for j in xrange(k)] for i in xrange(n)]
def wrapper(n, k, pre):
if cache[n-1][k-1][pre-1] is None:
cache[n-1][k-1][pre-1] = f(n, k, pre)
return cache[n-1][k-1][pre-1]
return wrapper
#memoize
def _part(n, k, pre):
if n <= 0:
return []
if k == 1:
if n <= pre:
return [(n,)]
return []
ret = []
for i in xrange(min(pre, n), 0, -1):
ret += [(i,) + sub for sub in _part(n-i, k-1, i)]
return ret
return _part(n, k, n)
First I want to thanks everyone for their contribution.
I arrived here needing an algorithm for generating integer partitions with the following details :
Generate partitions of a number into EXACTLY k parts but also having MINIMUM and MAXIMUM constraints.
Therefore, I modified the code of "Snakes and Coffee" to accommodate these new requirements:
def partition_min_max(n, k, l, m):
''' n is the integer to partition, k is the length of partitions,
l is the min partition element size, m is the max partition element size '''
if k < 1:
raise StopIteration
if k == 1:
if n <= m and n>=l :
yield (n,)
raise StopIteration
for i in range(l,m+1):
for result in partition_min_max(n-i, k-1, i, m):
yield result+(i,)
>>> x = list(partition_min_max(20 ,3, 3, 10 ))
>>> print(x)
>>> [(10, 7, 3), (9, 8, 3), (10, 6, 4), (9, 7, 4), (8, 8, 4), (10, 5, 5), (9, 6, 5), (8, 7, 5), (8, 6, 6), (7, 7, 6)]
Building upon previous answer with maximum and minimum constraints, we can optimize it be a little better . For eg with k = 16 , n = 2048 and m = 128 , there is only one such partition which satisfy the constraints(128+128+...+128). But the code searches unnecessary branches for an answer which can be pruned.
def partition_min_max(n,k,l,m):
#n is the integer to partition, k is the length of partitions,
#l is the min partition element size, m is the max partition element size
if k < 1:
return
if k == 1:
if n <= m and n>=l :
yield (n,)
return
if (k*128) < n: #If the current sum is too small to reach n
return
if k*1 > n:#If current sum is too big to reach n
return
for i in range(l,m+1):
for result in partition_min_max(n-i,k-1,i,m):
yield result+(i,)

Categories

Resources