most recent 30 from stackoverflow.com2026-03-01T05:30:13Zhttps://stackoverflow.com/feeds/question/64336862https://creativecommons.org/licenses/by-sa/4.0/rdfhttps://stackoverflow.com/q/64336862-3Jacob Georgehttps://stackoverflow.com/users/89191432020-10-13T14:10:49Z2020-10-13T15:22:39Z
<p>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:</p>
<p>1-Gets a list as input</p>
<p>2-Create a combination list with 11 numbers</p>
<p>3-Check the list for a specific condition</p>
<p>4-If yes, return the list's sum</p>
<p>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.</p>
<p>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</p>
https://stackoverflow.com/questions/64336862/-/64337132#643371320David Culbrethhttps://stackoverflow.com/users/48022592020-10-13T14:25:00Z2020-10-13T14:25:00Z<p>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.</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
https://stackoverflow.com/questions/64336862/-/64337185#643371850GalSuchetzkyhttps://stackoverflow.com/users/141321042020-10-13T14:28:05Z2020-10-13T14:28:05Z<p>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).</p>