Generating all possible permutations for a given base and number of digits

后端 未结 3 1156
故里飘歌
故里飘歌 2020-12-20 23:02

I\'m sure this is pretty simple, but I\'m stumped for a way to do this. Essentially if I have an array with P collumns and V^P rows, how can I fill in all the combinations,

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-20 23:24

    If there is varying number of options in each "digit", this code can be used.

    Maybe in some optimization tool there exist algorithm to do this, because it could be useful for brute force method. Code below adds a column which shows "the value of biggest digit", it can be ignored:

    import numpy as np
    val=np.arange(15)
    options=[2,2,3]
    print(val)
    print(options)
    opt = options + [1] # Assumes options to be a list
    opt_cp = np.flip(np.cumprod(np.flip(np.array(opt))))
    ret = np.floor_divide(val[:,np.newaxis], opt_cp[np.newaxis,:])
    ret[:,1:] = np.remainder(ret[:,1:], np.array(opt[:-1])[np.newaxis,:])
    inds = ret[:,1:]
    print(inds)
    

提交回复
热议问题