-3

So I have a 360 element list that I want to find the highest sum of 11 numbers combination, but with a condition.To make it a bit clearer:

1-Gets a list as input

2-Create a combination list with 11 numbers

3-Check the list for a specific condition

4-If yes, return the list's sum

I tried to use itertools.combination then check for the condition but it took so long as my list is really big.So I'm wondering if there's a way to check for the condition first rather than creating all the combinations then filtering them out.

EDIT: Guys I think you didn't get my question quite well.I want to get the list's combination first(like permutation), not just the highest 11 numbers

2
  • 1
    What is your condition exactly? that the sum should be maximum? Commented Oct 13, 2020 at 14:13
  • 1
    Why not sorted the list, descending, and then pick the first 11? If you need the indices, you can just find the numbers that you need from the original list. Commented Oct 13, 2020 at 14:15

2 Answers 2

0

Why not sorted the list, descending, and then pick the first 11? If you need the indices, you can just find the numbers that you need from the original list.

import random
import pprint

original_items = random.choices(range(999), k=360)
pprint.pprint(original_items)

highest_11 = sorted(original_items, reverse=True)[:11]
pprint.pprint(highest_11)
Sign up to request clarification or add additional context in comments.

2 Comments

I meant highest 11 combination.Like in itertools.combination(list, 11)
I think the point is that the itertools approach is just inherently slow. if you need better runtimes, you're going to need to change your approach.
0

Generally, sort the list and find the first 11 numbers that satisfy your condition. Even if your condition is non deterministic, you can still probably reduce the runtime to linear for the search itself (and thus the runtime will depend on the condition).

1 Comment

I meant highest 11 combination.Like in itertools.combination(list, 11)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.