Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

arrays - Algorithm for Number series row and column

I am trying to create a simple Algorithm in Dart but I think the programming language doesn't matter it is more about the Algorithm:

I am trying to make 2 lists of pairs of numbers depending on "row" and "column" for example:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're probably looking for the modulo operator %

Directly, it can create a repeating series based upon the remainder of dividing by the value

For example, you could place your values into the Nth list in a list of lists (whew)

>>> cols = [[],[],[]]
>>> for x in range(12):
...     cols[x % 3].append(x)
...
>>> cols
[[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]
0%5   -->  0
1%5   -->  1
...
11%5  -->  1

To massage values when you have (for example) some increment of 1, you can do a little math

>>> for x in range(12):
...     print("x={} is in column {}".format(x + 1, (x % 3) + 1 ))
...
x=1 is in column 1
x=2 is in column 2
x=3 is in column 3
x=4 is in column 1
x=5 is in column 2
x=6 is in column 3
x=7 is in column 1
x=8 is in column 2
x=9 is in column 3
x=10 is in column 1
x=11 is in column 2
x=12 is in column 3

Note you may need to do some extra work to either

  • know how many values you need to fill all the columns
  • count and stop when you have enough rows

Examples are in Python because I'm most familiar with it
Note that work like [[],[],[]] should largely be avoided if a better collection is available (perhaps a dict of lists with column-name keys or Pandas DataFrame), but it makes for a good illustration


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...