Combination in Python - python-3.x

I am trying to understand the Algo of combinations function in python
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
But I am unable to understand why if indices[i] != i + n - r: and
for j in range(i+1, r):
indices[j] = indices[j-1] + 1is used in this function what is their role.
Please help me .

I think a solution like this will do, what you are trying to achieve
combinationDict = []
def combinations(iterable, r, cnt, subPart):
if cnt > r:
return
if cnt == r:
combinationDict.append(subPart)
return
if (len(iterable) < r):
return
i = 0
while i < len(iterable):
combinations(iterable[:i]+iterable[i+1:], r, cnt+1, subPart+iterable[i])
combinations(iterable[:i]+iterable[i+1:], r, cnt, subPart)
i += 1
combinations("ABCD", 2, 0, "")
print(combinationDict)
Basically main aim of the above solution is, at each step of recursion, either to add a particular element at i or don't add it to the combination.
Let's say for example of ABCD and 2, we start with A, then we add B, then we see size is 2, we return, we add C to A as we return back to the recursive call.
Now this, solution has exponential time complexity as it is a brute force solution.
Hope this helps!

Related

PuLP Optimization problem - how to add constraints?

I am trying to solve a linear programming problem with the following constraints:
for some given values of N and T. So suppose N={1,2} and T={1,2}
It is easy to write out for small |N|, but is impossible to write as N becomes large.
Im confused as to how to actually code these sums by indexing on my objective variables.
I used the following to create my objective variables:
for t in range(1,T+1):
for i in range(1,n+1):
for j in range(1,n+1):
# Skip the x_i,i,t entries
if j == i:
continue
element = "x"+str(i)+','+str(j)+','+str(t)
x_ijt_holding.append(element)
x_ijt =[]
for i in x_ijt_holding:
x_ijt.append(pulp.LpVariable(i, cat = "Binary"))
Initially I thought I could just define each constraint with LpVariable, but I realized the solver doesn't like that.
For example I did:
# Constraint 2 enter entries
x_ijt_2_holding = []
for j in range(1,n+1):
for i in range(1,j):
equations_2 = []
equations_3 = []
for t in range(1, T+1):
equations_2.append("x"+str(i)+','+str(j)+','+str(t))
equations_2.append("x"+str(j)+','+str(i)+','+str(t))
x_ijt_2_holding.append(equations_2)
# Constraint 2 as LpVariable:
for i in x_ijt_2_holding:
temp = []
for sublist in i:
temp.append(pulp.LpVariable(sublist, cat = "Binary"))
x_ijt_con2.append(temp)
So how would I then code the constraints into the problem?
Constraints defines relationships on previously defined variable written as equations. When you are building your constraints you need to reference the variables you defined in your first part. Saving your variables in dicts makes it much easier to reference your variables later.
Take a look at this
"""
Optimizing
sum(x[i][j][t] + x[j][i][t]) ==1 where j in N \ {i} for each i in N and for each t in T
sum(x[i][j][t] + x[j][i][t]) ==2 where t in T for each i,j in N, i < j
programmer Michael Gibbs
"""
import pulp
N = [1,2]
T = [1,2]
model = pulp.LpProblem("basis")
# variables
# x[i][j][t]
x = {
i:{
j:{
t:pulp.LpVariable(
'x_' + str(i) + '_' + str(j) + '_' + str(t),
cat=pulp.LpBinary
)
for t in T
}
for j in N if j != i
}
for i in N
}
# constraints
#sum(x[i][j][t] + x[j][i][t]) ==1 where j in N \ {i} for each i in N and for each t in T
for i in N:
for t in T:
c = pulp.lpSum([x[i][j][t] + x[j][i][t] for j in N if j != i]) == 1
model += c
#sum(x[i][j][t] + x[j][i][t]) ==2 where t in T for each i,j in N, i < j
for i in N:
for j in N:
if i < j:
c = pulp.lpSum([x[i][j][t] + x[j][i][t] for t in T]) == 2
model += c
# no objective
model.solve()
print("i","j","t","value")
print('-------------------')
[print(i,j,t, pulp.value(x[i][j][t])) for i in N for j in N if i != j for t in T]

Count the number of possible ways to solve an equation

This question was asked in a challenge in HackerEarth:
Mark is solving an interesting question. He needs to find out number
of distinct ways such that
(i + 2*j+ k) % (x + y + 2*z) = 0, where 1 <= i,j,k,x,y,z <= N
Help him find it.
Constraints:
1<= T<= 10
1<=N<= 1000
Input Format:
First line contains T, the number of test cases. Each of the test case
contains a single integer,N in a separate line.
Output Format:
For each test case , output in a separate line, the number of distinct
ways.
Sample Input
2
1
2
Sample Output
1
15
Explanation
In the first case, the only possible way is i = j = k = x =y = z = 1
I am not getting any way how to solve this problem, I have tried one and I know it's not even close to the question.
import random
def CountWays (N):
# Write your code here
i = random.uniform(1,N)
j = random.uniform(1,N)
k = random.uniform(1,N)
x = random.uniform(1,N)
y = random.uniform(1,N)
z = random.uniform(1,N)
d = 0
for i in range(N):
if (i+2*j+k)%(x+y+2*z)==0:
d += 1
return d
T = int(input())
for _ in range(T):
N = int(input())
out_ = CountWays(N)
print (out_)
My Output
0
0
Instead it should give the output
1
15
The value of the numerator (num) can range from 4 to 4N. The value of the denominator (dom) can range from 4 to num. You can split your problem into two smaller problems: 1) How many values of the denominator is a given value of the numerator divisible by? 2) How many ways can a given denominator and numerator be constructed?
To answer 1) we can simply loop through all the possible values of the numerator, then loop over all the values of the denominator where numerator % denominator == 0. To answer 2) we can find all the partitions of the numerator and denominator that satisfies the equality and constraints. The number of ways to construct a given numerator and denominator will be the product of the number of partitions of each.
import itertools
def divisible_numbers(n):
"""
Get all numbers with which n is divisible.
"""
for i in range(1,n+1):
if n % i == 0:
yield i
if i >= n:
break
def get_partitions(n):
"""
Generate ALL ways n can be partitioned into 3 integers.
Modified from http://code.activestate.com/recipes/218332-generator-for-integer-partitions/#c9
"""
a = [1]*n
y = -1
v = n
while v > 0:
v -= 1
x = a[v] + 1
while y >= 2 * x:
a[v] = x
y -= x
v += 1
w = v + 1
while x <= y:
a[v] = x
a[w] = y
if w == 2:
yield a[:w + 1]
x += 1
y -= 1
a[v] = x + y
y = a[v] - 1
if w == 3:
yield a[:w]
def get_number_of_valid_partitions(num, N):
"""
Get number of valid partitions of num, given that
num = i + j + 2k, and that 1<=i,j,k<=N
"""
n = 0
for partition in get_partitions(num):
# This can be done a bit more cleverly, but makes
# the code extremely complicated to read, so
# instead we just brute force the 6 combinations,
# ignoring non-unique permutations using a set
for i,j,k in set(itertools.permutations(partition)):
if i <= N and j <= N and k <= 2*N and k % 2 == 0:
n += 1
return n
def get_number_of_combinations(N):
"""
Get number of ways the equality can be solved under the given constraints
"""
out = 0
# Create a dictionary of number of valid partitions
# for all numerator values we can encounter
n_valid_partitions = {i: get_number_of_valid_partitions(i, N) for i in range(1,4*N+1)}
for numerator in range(4,4*N+1):
numerator_permutations = n_valid_partitions[numerator]
for denominator in divisible_numbers(numerator):
denominator_permutations = n_valid_partitions[denominator]
if denominator < 4:
continue
out += numerator_permutations * denominator_permutations
return out
N = 2
out = get_number_of_combinations(N)
print(out)
The scaling of the code right now is very poor due to the way the get_partitions and the get_number_of_valid_partitions functions interact.
EDIT
The following code is much faster. There's a small improvement to divisible_numbers, but the main speedup lies in get_number_of_valid_partitions not creating a needless amount of temporary lists as it has now been joined with get_partitions in a single function. Other big speedups comes from using numba. The code of get_number_of_valid_partitions is all but unreadable now, so I've added a much simpler but slightly slower version named get_number_of_valid_partitions_simple so you can understand what is going on in the complicated function.
import numba
#numba.njit
def divisible_numbers(n):
"""
Get all numbers with which n is divisible.
Modified fromĀ·
"""
# We can save some time by only looking at
# values up to n/2
for i in range(4,n//2+1):
if n % i == 0:
yield i
yield n
def get_number_of_combinations(N):
"""
Get number of ways the equality can be solved under the given constraints
"""
out = 0
# Create a dictionary of number of valid partitions
# for all numerator values we can encounter
n_valid_partitions = {i: get_number_of_valid_partitions(i, N) for i in range(4,4*N+1)}
for numerator in range(4,4*N+1):
numerator_permutations = n_valid_partitions[numerator]
for denominator in divisible_numbers(numerator):
if denominator < 4:
continue
denominator_permutations = n_valid_partitions[denominator]
out += numerator_permutations * denominator_permutations
return out
#numba.njit
def get_number_of_valid_partitions(num, N):
"""
Get number of valid partitions of num, given that
num = i + j + 2l, and that 1<=i,j,l<=N.
"""
count = 0
# In the following, k = 2*l
#There's different cases for i,j,k that we can treat separately
# to give some speedup due to symmetry.
#i,j can be even or odd. k <= N or N < k <= 2N.
# Some combinations only possible if num is even/odd
# num is even
if num % 2 == 0:
# i,j odd, k <= 2N
k_min = max(2, num - 2 * (N - (N + 1) % 2))
k_max = min(2 * N, num - 2)
for k in range(k_min, k_max + 1, 2):
# only look at i<=j
i_min = max(1, num - k - N + (N + 1) % 2)
i_max = min(N, (num - k)//2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
# if i == j, only one permutations
# otherwise two due to symmetry
if i == j:
count += 1
else:
count += 2
# i,j even, k <= N
# only look at k<=i<=j
k_min = max(2, num - 2 * (N - N % 2))
k_max = min(N, num // 3)
for k in range(k_min, k_max + 1, 2):
i_min = max(k, num - k - N + N % 2)
i_max = min(N, (num - k) // 2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == j == k:
# if i == j == k, only one permutation
count += 1
elif i == j or i == k or j == k:
# if only two of i,j,k are the same there are 3 permutations
count += 3
else:
# if all differ, there are six permutations
count += 6
# i,j even, N < k <= 2N
k_min = max(N + 1 + (N + 1) % 2, num - 2 * N)
k_max = min(2 * N, num - 4)
for k in range(k_min, k_max + 1, 2):
# only look for i<=j
i_min = max(2, num - k - N + 1 - (N + 1) % 2)
i_max = min(N, (num - k) // 2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == j:
# if i == j, only one permutation
count += 1
else:
# if all differ, there are two permutations
count += 2
# num is odd
else:
# one of i,j is even, the other is odd. k <= N
# We assume that j is odd, k<=i and correct the symmetry in the counts
k_min = max(2, num - 2 * N + 1)
k_max = min(N, (num - 1) // 2)
for k in range(k_min, k_max + 1, 2):
i_min = max(k, num - k - N + 1 - N % 2)
i_max = min(N, num - k - 1)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == k:
# if i == j, two permutations
count += 2
else:
# if i and k differ, there are four permutations
count += 4
# one of i,j is even, the other is odd. N < k <= 2N
# We assume that j is odd and correct the symmetry in the counts
k_min = max(N + 1 + (N + 1) % 2, num - 2 * N + 1)
k_max = min(2 * N, num - 3)
for k in range(k_min, k_max + 1, 2):
i_min = max(2, num - k - N + (N + 1) % 2)
i_max = min(N, num - k - 1)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
count += 2
return count
#numba.njit
def get_number_of_valid_partitions_simple(num, N):
"""
Simpler but slower version of 'get_number_of_valid_partitions'
"""
count = 0
for k in range(2, 2 * N + 1, 2):
for i in range(1, N + 1):
j = num - i - k
if 1 <= j <= N:
count += 1
return count
if __name__ == "__main__":
N = int(sys.argv[1])
out = get_number_of_combinations(N)
print(out)
The current issue with your code is that you've picked random numbers once, then calculate the same equation N times.
I assume you wanted to generate 1..N for each individual variable, which would require 6 nested loops from 1..N, for each variable
Now, that's the brute force solution, which probably fails on large N values, so as I commented, there's some trick to find the multiples of the right side of the modulo, then check if the left side portion is contained in that list. That would only require two triple nested lists, I think
(i + 2*j+ k) % (x + y + 2*z) = 0, where 1 <= i,j,k,x,y,z <= N
(2*j + i + k) is a multiple of (2*z + x + y)
N = 2
min(2*j + i + k) = 4
max(2*j + i + k) = 8
ways to make 4: 1 * 1 = 1
ways to make 5: 2 * 2 = 4
ways to make 6: 2 * 2 = 4
ways to make 7: 2 * 2 = 4
ways to make 8: 1 * 1 = 1
Total = 14
But 8 is a multiple of 4 so we add one more instance for a total of 15.

Where should I put the count in this tim sort algorithm, to accurately compare runtime to other algorithms

I've written a Timsort sorting algorithm for a computer science class, I would like to be able to compare the runtime to other similar algorithms, such as merge sort for instance. However, I am not sure where I should put the count (ie: count +=1)within the code to have an accurate run time. Any help would be much appreciated.
RUN = 32
def insertion_sort(arr, left, right):
for i in range(left + 1, right + 1):
temp = arr[i]
j = i - 1
while (arr[j] > temp and j >= left):
arr[j + 1] = arr[j]
arr[j] = temp
j -= 1
def merge(arr, left, right, count):
c = 0
index = count
length = len(left) + len(right)
while left and right:
if left[0] < right[0]:
arr[index] = left.pop(0)
c += 1
index += 1
else:
arr[index] = right.pop(0)
c += 1
index += 1
if len(left) == 0:
while c < length:
arr[index] = right.pop(0)
c += 1
index += 1
elif len(right) == 0:
while c < length:
arr[index] = left.pop(0)
c += 1
index += 1
def tim_sort(arr):
n = len(arr)
for i in range(0, n, RUN):
insertion_sort(arr, i, min((i + (RUN - 1)), (n - 1)))
size = RUN
while size < n:
for left in range(0, n, 2 * size):
if (left + size > n):
merge(arr, arr[left:n], [], left)
else:
left_sub_arr = arr[left:(left + size)]
right_sub_arr = arr[(left + size):min((left + 2 * size), n)]
merge(arr, left_sub_arr, right_sub_arr, left)
size *= 2
return arr

all permutations of string without using itertools

All possible strings of any length that can be formed from a given string
Input:
abc
Output:
a b c abc ab ac bc bac bca
cb ca ba cab cba acb
I have tried using this but it's limited to string abc, I want to generalize it like if input 'abcd' i will provide me output for the same.
def perm_main(elems):
perm=[]
for c in elems:
perm.append(c)
for i in range(len(elems)):
for j in range(len(elems)):
if perm[i]!= elems[j]:
perm.append(perm[i]+elems[j])
level=[elems[0]]
for i in range(1,len(elems)):
nList=[]
for item in level:
#print(item)
nList.append(item+elems[i])
#print(nList)
for j in range(len(item)):
#print(j)
nList.append(item[0:j]+ elems[i] + item[j:])
#print(nList)
level=nList
perm = perm + nList
return perm
You may not need itertools, but you have the solution in the documentation, where itertools.permutations is said to be roughly equivalent to:
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
Or using product:
def permutations(iterable, r=None):
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
for indices in product(range(n), repeat=r):
if len(set(indices)) == r:
yield tuple(pool[i] for i in indices)
They are both generators so you will need to call list(permutations(x)) to retrieve an actual list or substitute the yields for l.append(v) where l is a list defined to accumulate results and v is the yielded value.
For all the possible sizes ones, iterate over them:
from itertools import chain
check_string = "abcd"
all = list(chain.from_iterable(permutations(check_string , r=x)) for x in range(len(check_string )))
Partial recursive solution. You just need to make it work for different lengths:
def permute(pre, str):
n = len(str)
if n == 0:
print(pre)
else:
for i in range(0,n):
permute(pre + str[i], str[0:i] + str[i+1:n])
You can call it using permute('', 'abcd'), or have another method to simplify things
def permute(str):
permute('', str)
Answer borrowed from here.
In general, you will have better luck translating code from C/Cpp/Java solutions to Python because they generally implement things from scratch and do things without much need of libraries.
UPDATE
Full solution:
def all_permutations(given_string):
for i in range(len(given_string)):
permute('', given_string, i+1)
def permute(prefix, given_string, max_len):
if len(given_string) <= 0 or len(prefix) >= max_len:
print(prefix)
else:
for i in range(len(given_string)):
permute(prefix + given_string[i], given_string[:i] + given_string[i+1:], max_len)
>>> all_permutations('abc')
a
b
c
ab
ac
ba
bc
ca
cb
abc
acb
bac
bca
cab
cba

Need Optimized Python Solution for Leetcode 3Sum Question

Here's the Problem Statement:
Given an array nums of n integers,
are there elements a, b, c in nums such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[[-1, 0, 1],[-1, -1, 2]]
I'm solving Leetcode 3Sum problem right now and getting Time Limit Exceeded error for the below Code:
class Solution:
def threeSum(self, nums):
triplets=[]
nums.sort()
for i in range(len(nums)-1):
l=i+1
r=len(nums)-1
while l<r:
sum=nums[i]+nums[l]+nums[r]
if sum==0:
if not [nums[i],nums[l],nums[r]] in triplets:
triplets+=[[nums[i],nums[l],nums[r]]]
if sum<0:
l+=1
else:
r-=1
return triplets
Can anyone tell me where can I optimize this code?
Your algorithm looks optimal in general (slightly better complexity exists but approach perhaps is too complex for practical purposes).
But seems that searching of sublist in list is rather slow operation (probably linear for unsorted)
Use dictionary instead and extract triplets at the end.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
result = []
N = len(nums)
for i in range(N):
if i > 0 and nums[i] == nums[i-1]:
continue
target = -nums[i]
l = i + 1
r = N - 1
while l < r:
if nums[l] + nums[r] == target:
result.append([nums[l],nums[r],nums[i]])
l = l + 1
while l <= r and nums[l] == nums[l - 1]:
l = l + 1
elif nums[l] + nums[r] > target:
r = r - 1
else:
l = l + 1
return result

Resources