Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to implement a function, generate_perm(m,n), where it takes two arguments, one is for numbers to permutate (m) and second is to what the sum of the permutation must equate to (n).
def generate_perm(m,n):
'''
implement this
'''
return
generate_perm(2,5) should output
[(1,4), (2,3), (3,2) (4,1)]
and
generate_perm(3,5) should output:
[(1,1,3), (1,2,2), (1,3,1), (2,1,2), (2,2,1), (3,1,1)]
EDIT
i havent gotten far
def generate_permutations(m, n):
all = []
cur = [0 for i in range(m)]
len_perm = m*2
while True:
for i in range(m):
if cur[i] == 0: # initial case
if i != m-1:
cur[i] = 1
else:
cur[i] = n-m
all.append(cur)
if len(all) >= len_perm:
break
return all
Consider a list l = [1,2,3,4,5]
You can get all the permutations with itertools.permutations
p = itertools.permutations(list(range(1,6)),2)
and then filter them
my_elems = [el for el in p if sum(el) == 5]
outputs
[(1, 4), (2, 3), (3, 2), (4, 1)]
Looking at the second example you give, I think what you want is a product, not permutations:
p = itertools.product(list(range(1,6)),repeat=3)
my_elems = [el for el in p if sum(el) == 5]
#[(1, 1, 3), (1, 2, 2), (1, 3, 1), (2, 1, 2), (2, 2, 1), (3, 1, 1)]
and which works also for the first case.
One simple recursive approach without any libraries:
def perm(m, n):
if m == 1: # base case
return [(n,)]
perms = []
for s in range(1, n): # combine possible start values: 1 through n-1 ...
for p in perm(m-1, n-s): # ... with all appropriate smaller perms
perms.append((s,) + p)
return perms
>>> perm(1, 5)
[(5,)]
>>> perm(2, 5)
[(1, 4), (2, 3), (3, 2), (4, 1)]
>>> perm(3, 5)
[(1, 1, 3), (1, 2, 2), (1, 3, 1), (2, 1, 2), (2, 2, 1), (3, 1, 1)]
Related
For example given points: points = [(1,2),(3,4),(5,7),(4,7),(6,7)], i need the program to find all combination such that there's a path between points (let's say 7 is the destination we want to reach)
so the output would be: [(1,2),(3,4),(5,7)] [(1,2),(3,4),(4,7)] [(1,2),(3,4),(6,7)]
u get the idea?
I'm really stuck with it an i cannot find something similar on the internet.
Truly, I don't get why you need this, but it is a simple task.
source_list = [(1,2),(3,4),(5,7),(4,7),(6,7)]
final_point = 7 # ??? did not get the logic, btw
def some_magic_shit(lst, fp):
out = []
while True:
way = []
for k, v in enumerate(lst):
way.append(v)
if v[1] >= fp:
lst.pop(k)
out.append(way)
if k >= len(lst):
return out
else:
break
print(some_magic_shit(source_list, final_point)) # [[(1, 2), (3, 4), (5, 7)], [(1, 2), (3, 4), (4, 7)], [(1, 2), (3, 4), (6, 7)]]
The code above should be rewritten with the proper logic.
It only uses Y axis as a final point.
I want to use python to define one set in terms of another, as follows: For some set N that consists of sets, define C as the set such that an element n of N is in C just in case any two elements of n both satisfy some specific condition.
Here is the particular problem I need to solve. Consider the power set N of the set of ordered pairs of elements in x={1,2,3,4,5,6}, and the following subsets of N:
i = {{1,1},{2,2},{3,4},{4,3},{5,6},{6,5}}
j = {{3,3},{4,4},{1,2},{2,1},{5,6},{6,5}}
k = {{5,5},{6,6},{1,2},{2,1},{3,4},{4,3}}
Using python, I want to define a special class of subsets of N: the subsets of N such that any two of their members are both either in i, j, or k.
More explicitly, I want to define the set terms: C = {n in N| for all a, b in n, either a and b are both in i or a and b are both in j or a and b are both in k}.
I'm attaching what I tried to do in Python. But this doesn't give me the right result: the set C I'm defining here is not such that any two of its members are both either in i, j, or k.
Any leads would be much appreciated!
import itertools
def powerset(iterable):
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
x = [1,2,3,4,5,6]
ordered_pairs = [[j,k] for j in x for k in x if k>=j]
powers = list(powerset(ordered_pairs))
i = [[1,1],[2,2],[3,4],[4,3],[5,6],[6,5]]
j = [[3,3],[4,4],[1,2],[2,1],[5,6],[6,5]]
k = [[5,5],[6,6],[1,2],[2,1],[3,4],[4,3]]
M = [i,j,k]
C = []
for n in powers:
for a in n:
for b in n:
for m in M:
if a in m:
if b in m:
if a != b:
C.append(n)
if len(n) == 1:
C.append(n)
First of all, note that the ordered pairs you list are sets, not pairs. Use tuples, since they're hashable, and you'll be able to easily generate the power set using itertools. With that done, you have an easier time identifying the qualifying subsets.
The code below implements that much of the process. You can accumulate the hits at the HIT line of code. Even better, you can collapse the loop into a nested comprehension using any to iterate over the three zone sets.
test_list = [
set(((1,1),(2,2))), # trivial hit on i
set(), # trivial miss
set(((1, 1), (4, 4), (6, 6))), # one element in each target set
set(((3, 3), (6, 2), (4, 4), (2, 2))), # two elements in j
]
i = set(((1,1),(2,2),(3,4),(4,3),(5,6),(6,5)))
j = set(((3,3),(4,4),(1,2),(2,1),(5,6),(6,5)))
k = set(((5,5),(6,6),(1,2),(2,1),(3,4),(4,3)))
zone = [i, j, k]
for candidate in test_list:
for target in zone:
overlap = candidate.intersection(target)
if len(overlap) >= 2:
print("HIT", candidate, target)
break
else:
print("MISS", candidate)
Output:
HIT {(1, 1), (2, 2)} {(5, 6), (4, 3), (2, 2), (3, 4), (1, 1), (6, 5)}
MISS set()
MISS {(4, 4), (1, 1), (6, 6)}
HIT {(6, 2), (3, 3), (4, 4), (2, 2)} {(1, 2), (3, 3), (5, 6), (4, 4), (2, 1), (6, 5)}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Write a function triplets that takes a number n as argument and returns a list of triplets such that sum of first two elements of the triplet equals the third element using numbers below n. Please note that (a, b, c) and (b, a, c) represent same triplet.
triplets(5)
[(1, 1, 2), (1, 2, 3), (1, 3, 4), (2, 2, 4)]
You can do this with a list comprehension:
def triplets(n):
return [ (a,c-a,c) for c in range(2,n) for a in range(1,c//2+1) ]
Can you try this?
def write_triplets(n):
to_return = []
if n >= 2:
for i in range(2, n):
for j in range(1, i/2+1):
triplet = (j, i-j, i)
to_return.append(triplet)
return to_return
write_triplets(5)
OUTPUT:
# Result: [(1, 1, 2), (1, 2, 3), (1, 3, 4), (2, 2, 4)] #
This question already has answers here:
How do I generate all permutations of a list?
(40 answers)
Closed 3 years ago.
trying to make a function permutations(s) that takes a set of elemnts and returns a collection of all its permutations, where the permutations are of type tuple.
Here is my code:
def permutations(s):
str1 = list(s)
if len(str1) <= 1:
print(s)
else:
for i in range(0, len(s)):
str1[0], str1[i] = str1[i], str1[0]
permutations(str1[1:])
str1[0], str1[i] = str1[i], str1[0]
given this input
print(sorted(permutations({1,2,3})))
it should return
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
but after alot of headache i can only seem to get
[3][2][3][1][1][2]
You can use permutations from itertools in the standard library to compute all permutations
from itertools import permutations
out = list(permutations({1,2,3}))
print(out)
#Output
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
You are probably looking for an algorithm, e.g. recursive algorithm. Some times ago I wrote it (as exercise):
def get_permutations(array):
result = list()
def permute(x, index, acc=[0] * len(array)):
if index == len(array):
result.append(acc[:])
acc = list()
return None
for j in range(len(x)):
acc[index] = x[j]
_x = x[:]
_x.pop(j)
permute(_x, index + 1, acc)
permute(array, 0)
return result
How will I do the following?
class a:
def get(self):
return (1, 2)
i = a() #this is variable that i wont have access directly and should be accessed only from list P below
P = [i, i, i, i]
Q = [P, P, P, P]
I want is
L = [list of all i.get() results iterating through Q and P]
for eg
L = [px.get() for px in P for P in Q] # I want something like this
i.e
L = [(1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2)]
Use a list comprehension:
L = [i.get() for P in Q for i in P]
This gives you [Q[0][0].get(), Q[0][1].get(), ..., Q[-1][-2].get(), Q[-1][-1].get()]
The double loop is just like a nested for loop outside of a list comprehension, the above is the equivalent of:
L = []
for P in Q:
for i in P:
L.append(i.get())
The list comprehension follows the same order of loops, nesting is translated to loops being listed from left to right.
Replicating a list can be done with the * operator in Python (i.e. multiplying a list):
>>> [i.get()] * (len(P) + len(Q))
[(1,2), (1,2), (1,2), (1,2), (1,2), (1,2), (1,2), (1,2)]