perhaps you are already confused by the title. I have got an interesting problem, but it is beyond by knowledge. I urge for seeking help.
I will try my best to describe the question as clear as possible, I promise it's not very complex.
I have a set of natural numbers, in range of n, for instance:
[0, 1] : n = 2
[0, 1, 2] : n = 3
[0, 1, 2, 3, 4, 5] : n = 6
put the set of the natural numbers into groups with length l, the rule is that each number must be with any other any number only once, in other word non-repeated, which sequence does not matter.
for example:
# group [1, 2, 3] by a length of 2
[1, 2], [1, 3], [2, 3]
# group [1, 2, 3, 4] by a length of 3
[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]
The task is to represent sorted matrix by positional index of individual group, for instance:
# [0, 1, 2, 3]
# [0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]
# 2 x 6
# [0 0 0 1 1 2]
# [1 2 3 2 3 3]
# [0, 1, 2, 3]
# [0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]
# 3 x 4
# [0 0 0 1]
# [1 1 2 2]
# [2 3 3 3]
# [0, 1, 2, 3, 4]
# [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]
# 3 x 10
# [0 0 0 0 0 0 1 1 1 2]
# [1 1 1 2 2 3 2 2 3 3]
# [2 3 4 3 4 4 3 4 4 4]
In other words,
# given 2 x 6, generate:
# [0 0 0 1 1 2]
# [1 2 3 2 3 3]
# given 3 x 4, generate:
# [0 0 0 1]
# [1 1 2 2]
# [2 3 3 3]
# given 4 x 8, generate:
# [0 0 0 0 0 0 1 1 1 2]
# [1 1 1 2 2 3 2 2 3 3]
# [2 3 4 3 4 4 3 4 4 4]
the performance of the algorithm will be largely increased if the index is evaluated inside of the iteration.
In detail, with known
- current iteration index as horizontal index of the outcome matrix
- group length
l - how many groups will be generated (gained by following algorithm)
to find:
- a list of numbers of length
lrepresent current matrix composition.
# python 3.7
# how many groups will be generated
# is calculated by pascal triangle
import math
def factorial(num):
if num == 0:
return 1
val = 1
for i in range(num):
val *= i + 1
return val
def reduce(row, col):
return int(factorial(row) / (factorial(col) * factorial(row - col)))
def slicer(var, required):
# get the length of tha passed in list, for example [0, 1, 2, 3] -> 4
length = len(var)
how_many_groups = reduce(length, required)
for i in range(how_many_groups):
# iteration
pass
let's make it specific,
# 3 x 4:
# [0 0 0 1]
# [1 1 2 2]
# [2 3 3 3]
# when iteration i is equal to 0, I want to find
# [0 1 2]
# when iteration i is equal to 1, I want to find
# [0 1 3]
Thanks very much for reading to the end. I am truly sorry if it has confused you.
Any help is appreciable.