Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Math python question
#1
Hello, I'm new to this forum and I have a question. for those who are interested.
If i have a list of number ex: N=[1,2,3,4] and that I also have a list of numbers that cannot be together ex: P=[(1,2),(2,3),(3,4)] is it possible to make a cheap algorithm that won't test all the possible combinations to find a combination that works?

thanks for any response
Deon
Reply
#2
What do you mean by "cannot be put together" and "works"? Does [(1, 2), (25, 99), (8000, 9999)] work? Are you looking for no duplicates, or do the numbers also have to be sequential like [(1, 2), (3, 4), (5, 6)]? If sequential, do the numbers have to be in the sequential order, or is [(1, 5), (4, 2), (3, 6)] work?

More details please.
Reply
#3
You forgot to tell us what is a «combination that works», I suppose it is some set of non adjacent nodes, or a partition of such sets. In that case, a greedy coloring algorithm could help.
Reply
#4
Deon has two lists: The input list N and a list of forbidden combinations P.
So I guess he wants all permutations of N, but skip forbidden combinations. Am I right @Deonvek ?

An efficient way of comparing values is to use set(). So I would use this for storing forbidden combinations P.
N=[1,2,3,4]
P={(1,2),(2,3),(3,4)} # Make a set of forbidden combinations.

cartesian_product = set()

# You could also use itertools.product to find all permutations.
for i in N:
    for j in N:
        cartesian_product.add((i,j))

print("DEBUG ", cartesian_product)

result = cartesian_product - P
print("RESULT", result)
Output:
DEBUG {(4, 4), (2, 4), (1, 2), (2, 1), (3, 4), (4, 1), (3, 1), (4, 3), (1, 1), (1, 4), (4, 2), (2, 3), (3, 3), (2, 2), (3, 2), (1, 3)} RESULT {(2, 4), (2, 1), (4, 3), (3, 1), (2, 2), (3, 2), (4, 1), (1, 3), (4, 4), (1, 1), (1, 4), (4, 2), (3, 3)}
Now this is not exactly what you asked for, because you said:
(Apr-05-2023, 02:04 AM)Deonvek Wrote: algorithm that won't test all the possible combinations
Well you would have to test the combination at some point. Will the resultset be too large to fit in memory? Then you will have to skip the forbidden combinations in the nested loop.
Reply
#5
Does order matter? Is (2, 1) the same as (1, 2)?

Are duplicates such as (1, 1) allowed?

When you say you are looking for a combination that works, what do you mean by "combination"? Are you looking for pairs ((1, 3), (1, 4), (2, 4))?, enough pairs that every number in n is represented ((1, 3), (2, 4))?

Based on what ibreeden says and my guesses, are you looking for something like this:
n = (1, 2, 3, 4)
p = ((1, 2), (2, 3), (3, 4))

# Convert p to dictionary
bad = {a: {a} for a in n}
for a, b in p:
    bad[a].add(b)
    bad[b].add(a)
print("Bad", bad)

# Create a dictionary of numbers that can be paired.
good = {a: [b for b in n if b not in bad[a]] for a in n}
print("Good", good)

# Create all good pairs.  Assumes order is not important: (1, 2) == (2, 1).
# If order matters, leave out "if b > a".
good_pairs = [(a, b) for a, c in good.items() for b in c if b > a]
print("Good pairs", good_pairs)
Output:
Bad {1: {1, 2}, 2: {1, 2, 3}, 3: {2, 3, 4}, 4: {3, 4}} Good {1: [3, 4], 2: [4], 3: [1], 4: [1, 2]} Good pairs [(1, 3), (1, 4), (2, 4)]
Reply
#6
my interpreation of this task:

from itertools import permutations

res = []
nums = [1,2,3,4]
not_allowed = [(1,2),(2,3),(3,4)]

for perm in permutations(nums,2):
    if perm not in not_allowed:
        res.append(perm)
print(res)
Output:
[(1, 3), (1, 4), (2, 1), (2, 4), (3, 1), (3, 2), (4, 1), (4, 2), (4, 3)]
Reply
#7
The op requested a solution that doesn't test all possible combinations. I don't that meant testing more than all possible combinations. On the other hand, your solution does take the order of the pair into account if order is important.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,906 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  math.log versus math.log10 stevendaprano 10 2,441 May-23-2022, 08:59 PM
Last Post: jefsummers
  Math Package in python Uma 1 1,501 Dec-12-2021, 02:01 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,789 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  "SyntaxError: invalid syntax" running code in Doing Math With Python b saucerdesigner 2 2,753 Nov-03-2020, 04:23 PM
Last Post: saucerdesigner
  Programming Difficult math in Python Huntern 6 4,773 Oct-17-2019, 06:32 AM
Last Post: Huntern
  python 3 math formulas where to use () Python101 1 2,320 Jun-09-2019, 09:54 PM
Last Post: micseydel
  Python modeling a math solution Masterwoot 1 2,277 Mar-01-2019, 08:50 AM
Last Post: buran
  Python math error nadirdogan 3 3,376 Feb-11-2018, 10:56 AM
Last Post: nadirdogan
  Math problem python Dhaval 1 2,945 Jun-05-2017, 10:28 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020