Fundamentals 6 min read

Boost Your Python Combinatorial Solver: Faster Code with NumPy & Set Tricks

This article revisits a Python combinatorial challenge—selecting five numbers from a random fifteen‑element list that include consecutive values—presenting an optimized solution that leverages NumPy arrays, set operations, and intersection logic to cut execution time from twelve seconds to about one and a half seconds, while explaining the underlying reasoning.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Boost Your Python Combinatorial Solver: Faster Code with NumPy & Set Tricks

Hello, I'm a Python enthusiast.

In a recent discussion a fan asked how to choose five numbers from a random list of fifteen such that the chosen subset contains a pair of consecutive numbers (a and a+1), where a can be any element of the original list.

The original solution used basic Python and a Monte Carlo approach. Below is a refined version contributed by a senior developer, presented as pseudo‑code for clarity.

New Code

The following script generates 15 random numbers, repeatedly samples 5‑element subsets, and returns those containing a consecutive pair. It uses NumPy for fast array operations and set logic to reduce overhead.

# -*- coding: utf-8 -*-
import random
import numpy as np
import time

def get_random15():
    random_array = [np.array(random.sample(range(2000), 15)) for i in range(100000)]
    random5 = {get_random5(random15) for random15 in random_array}
    return [i for i in random5 if i]

def get_random5(random_15):
    random_5 = set(random_15[random.sample(range(15), 5)])  # replace choice with sample
    # check if the set contains a and a+1
    random_5_resp = {True if len(random_5.intersection({num, num + 1})) == 2 else False for num in random_5}
    return tuple(random_5) if True in random_5_resp else ()

if __name__ == '__main__':
    start_time = time.time()
    final_result = get_random15()
    print("共%d个符合题意的列表" % len(final_result))
    print("分别是:%s" % final_result)
    end_time = time.time()
    used_time = end_time - start_time
    print("本次程序用时:{}".format(time.strftime('%H(小时):%M(分钟):%S(秒)', time.gmtime(used_time))))

The script runs dramatically faster: the original implementation needed about 12 seconds, whereas this version completes in roughly 1.5 seconds.

Three key optimizations were applied:

Using NumPy arrays to generate the 15 random numbers eliminates costly Python loops.

Replacing a custom deduplication function with a set operation reduces both code complexity and execution time.

Employing set intersection to test for consecutive pairs is faster than multiple if statements.

Conclusion

This article demonstrates a concise, high‑performance solution to the combinatorial selection problem, showing how Python’s scientific‑computing libraries and set operations can dramatically accelerate brute‑force searches.

Thanks to the contributors for the original idea, the fan who posed the question, and the community members who discussed and refined the approach.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance OptimizationSet OperationsMonte CarloCombinatorial Algorithms
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.