user1348293
user1348293

Reputation: 85

generate unique serie of numbers in range (1, 2, 3...99; 1, 2, 3...99; ) with no duplicates

it's still not a year that i code in python in my spare time, and it is my first programming language. I need to generate serie of numbers in range ("1, 2, 3...99; 1, 2, 3...99;" ) and match them against a list. I managed to do this but the code looks pathetic and i failed in some tasks, for example by skipping series with duplicated/non unique numbers in an elegant way, or creating one single function that takes the length of the serie as parameter (for example 3 for 1-99, 1-99 and 1-99, 2 for 1-99 and 1-99 and so on) to avoid handwriting each serie's function.

What i have been capable of doing after exploring numpy multidimensional range ndindex (slow in my tests), creating pre filled lists (too huge), using set to uniquify (slow), tried all() for the "if n in y...", and many other things it, i have a very basic code which is also the fastest till now. After much changes, i'm simply back to the start, i moved the "if n != n" to the beginning of each for cycle in order to save loops and now have even no ideas on how to improve the function, or transforming it in a master function which generates n series of numbers. Any suggestion is really appreciated!

y = [#numbers]

def four(a,b):
    for i in range(a,b):
        for ii in range(a,b):
            if i != ii:
                for iii in range(a,b):
                    if i != iii and ii != iii:
                        for iiii in range(a,b):
                            if i != iiii and ii != iiii and iii != iiii:
                    if i in y and ii in y and iii in y and iiii in y:#exact match
                                    #do something
four(1,100)

Upvotes: 1

Views: 599

Answers (2)

user1348293
user1348293

Reputation: 85

thanks to user2357112 suggestion on using itertools.permutations(xrange(a, b), 4) I did the following and i'm very satisfied, it is fast and nice:

def solution(d, a, q):
    for i in itertools.permutations(xrange(d, a), q):
    #do something

solution(1,100,4)

Thanks to this brilliant community as well.

Upvotes: 2

VoidVolker
VoidVolker

Reputation: 1029

Create array[100], fill it numbers from 0 to 99. Then use random generator to mix them. And then just take needed number of numbers.

Upvotes: -1

Related Questions