How to list numbers by dividing them into n

Background I made the image of Irasutoya into a pixel art. (part1) When dividing a color into four I manually set the threshold as range_color = [0, 85, 170, 255] (or [i * int (255/3) for i in range (3)]). (Actually, it was divided into 3 parts and approximated to 4 values.)

This time it was just divisible, so there was no problem, but I thought how to design the algorithm if the range and the number of divisions were arbitrary and different values, so I summarized it.

In the above example, the range is 255 and the number of divisions is 3.

Method

It is not divisible cleanly and a remainder is generated. In that case, add +1 evenly to the first element.

So, how to do it

  1. Calculate the quotient and remainder from the range and the number of divisions
  2. Create an addition list for temporary calculations
  3. Calculate the element (boundary value) based on the addition list and push it.

It is the flow of.

Development

import sys

def main():
    if len(sys.argv) != 3:
        print("[USAGE] you give parameters this command.")
        print("[EXAMPLE] python main.py [range number] [n-division number]")
        print("[EXAMPLE] python main.py 255 4")
        sys.exit()

    r = int(sys.argv[1])  #range
    d = int(sys.argv[2])  #n Number of divisions
    q, mod = divmod(r, d) #Merchandise, calculation of remainder

    #Addition list
    plus = [q + 1 if i < mod else q for i, d in enumerate(range(d - 1))]
    print(plus)

    #output
    dst = []
    dst.append(plus[0])
    for i in range(len(plus)):
        dst.append(plus[0] + sum(plus[:1 + i]))
    print(dst)

if __name__ == '__main__':
    main()

Comment

\W $ python main2.py 255 6
[43, 43, 43, 42, 42]
[43, 86, 129, 172, 214, 256]

\W $ python main2.py 255 10
[26, 26, 26, 26, 26, 25, 25, 25, 25]
[26, 52, 78, 104, 130, 156, 181, 206, 231, 256]

The "addition list" is a list of the values of the intervals of each element. Based on this list, calculate each element with sum (plus [: 1 + i]).

Example) When you want to calculate the range 255, the number of divisions 10, and the 7th element Initial value 26 + [26, 26, 26, 26, 26, 25] total value = 181

It's the staff sequence that I learned in the high school sequence. : smirk_cat: Terra Natsu

a_n = a_1 + \sum_{k=1}^{n-1}b_k  \quad (n \geqq 2 )

PostScript When creating some content, you may think a little about the algorithm even on a small scale. As a developer, there is no shortage of writing because the items of qiita will increase. : robot:

Reference -Split integers as evenly as possible --You can do it with [(x + i) // n for i in range (n)]. This one is simpler.

Recommended Posts

How to list numbers by dividing them into n
[python] How to display list elements side by side
How to use list []
[Python] How to use list 1
[Python] How to make a list of character strings character by character
How to connect the contents of a list into a string
[Python] How to use list 3 Added
[FSL] How to peel off Atlas one by one and separate them
How to separate pipeline processing code into files by spider in Scrapy
[Python] How to display random numbers (random module)
[Python] How to convert a 2D list to a 1D list
How to decompose TTC fonts into TTFs.
Summary of how to use Python list
How to sublist a list including the elements that are left over after dividing it into specific lengths
How to convert m4a acquired by iTunes to wav
How to clear tuples in a list (Python)
[Django] How to get data by specifying SQL.
How to make scrapy JSON output into Japanese
How to erase the characters output by Python
[Python] How to sort instances by instance variables
How to count numbers in a specific range
Inspired by "How to make pure functional JavaScript"
How to create random numbers with NumPy's random module
How to remove duplicate elements in Python3 list