Which day I have been fighting with the algorithm, which I myself invented, but I can’t implement it, it sounds like this: There are number of illust , we know that the maximum number of lines of a two-dimensional post array can be no more than max_post , but no less than min_post . And the elements in these lines are not more than 7, moreover, the number of elements is always random from 2 to 7.
The goal is to fill the two-dimensional array with all the pictures, and the two-dimensional array was adjusted to the conditions that I said. Those. in the end, the elements in the lines, as it were, should stretch out (That's where I now have 7 elements, they could become two lines of 4 and 3 elements, then it would reach min_post ). I can't figure out how to do it ...
How can I optimize / rewrite / add it?
illust = 108 # All pictures a = [] # array elemets = 0 #element in array illust_number = 1 #the current number of images post = 0 # line in array max_post = 40 min_post = 30 while elemets < illust: if illust_number < illust: while illust_number < illust: if illust_number < illust: elemets = random.randint(2, 7) if illust_number + elemets <= illust: a.append([]) for c in range(elemets): a[post].append(illust_number) illust_number += 1 else: elemets = random.randint(1, illust - illust_number) a.append([]) for c in range(elemets): a[post].append(illust_number) illust_number += 1 else: break post += 1 else: break Conclusion:
[1, 2, 3, 4, 5] [6, 7, 8, 9, 10, 11] [12, 13] [14, 15, 16, 17] [18, 19, 20, 21, 22] [23, 24, 25] [26, 27, 28] [29, 30, 31, 32, 33, 34] [35, 36] [37, 38, 39, 40, 41, 42, 43] [44, 45, 46, 47, 48, 49, 50] [51, 52, 53, 54] [55, 56, 57, 58, 59, 60, 61] [62, 63, 64, 65, 66] [67, 68, 69, 70, 71, 72, 73] [74, 75] [76, 77] [78, 79, 80, 81, 82] [83, 84, 85, 86, 87, 88] [89, 90, 91] [92, 93, 94, 95, 96, 97, 98] [99, 100, 101, 102, 103, 104, 105] [106, 107]