most recent 30 from stackoverflow.com 2026-03-01T05:30:13Z https://stackoverflow.com/feeds/question/64336862 https://creativecommons.org/licenses/by-sa/4.0/rdf https://stackoverflow.com/q/64336862 -3 Jacob George https://stackoverflow.com/users/8919143 2020-10-13T14:10:49Z 2020-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#64337132 0 David Culbreth https://stackoverflow.com/users/4802259 2020-10-13T14:25:00Z 2020-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#64337185 0 GalSuchetzky https://stackoverflow.com/users/14132104 2020-10-13T14:28:05Z 2020-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>