0

Based on this answer: https://stackoverflow.com/a/34517664/8793029

I want the same thing but being the variable target a range of floats, I need to find all possible combinations multiplying instead of sum to reach a given range, for example between 1.8 and 2.2. Also the list being a list of classes.

To explain better, it's like a class bet where every bet is playing against X casino odds. I have a list of bets, so I need to find all possible combinations of those bets to reach between 1.8 and 2.2 odds. Combinations should output as class bet arrays and not as float arrays. The problem really is that if I get as output a list of odds I don't know to which bet belongs.

This is what I would need more or less but doing it bad:

import itertools
import numpy

class myclass:
    amount = 0.0

numbers = [myclass[1].amount, myclass[2].amount, myclass[3].amount, and so on...]
target = (1.8, 2.2)

result = [seq for i in range(len(numbers), 0, -1)
          for seq in itertools.combinations(numbers, i)
          if numpy.prod(seq).ISINRANGEOF(target)]

print(result)

How can I get all combinations of the given list to reach the desired range? So I can see all combinations of field amount being able to reach those classes individually, being result a list of possible combinations of my classes like:

print(result)
>> {myclass[2], myclass[1], myclass[3]}

Thanks in advance

  • Why not make a function that returns true or false based on the range? Basically `return low <= n <= high`. Then pass your sum and range to it in the comprehension. You could also just put the comparison in the comprehension, but it starts to get a little harder to read: `if low <= sum(seq) <= high` – Mark Feb 23 '22 at 18:12
  • Clearly an NP-hard problem. What sort of problem size are you dealing with? – John Coleman Feb 23 '22 at 18:37
  • To explain better, it's like a class bet where every bet is playing against X casino odds. I have a list of bets, so I need to find all possible combinations of those bets to reach between 1.8 and 2.2 odds. Combinations should output as class bet arrays and not as float arrays. If I get as output a list of odds I don't know to which bet belongs – Adrian Hernando Solanas Feb 23 '22 at 18:50
  • Remember the odd are not just the sum, it's the mean: `sum(seq)/len(seq)`. – Tim Roberts Feb 23 '22 at 19:10
  • Odds are the payment multiplier in betting – Adrian Hernando Solanas Feb 23 '22 at 20:01

0 Answers0