Quote:
Originally Posted by LoRd_MuldeR
Assuming row-major order as well as zero-based indexing:
Code:
row_index = address / num_columns; /* integer division (truncating) */
col_index = address % num_columns; /* modulo operation */
|
I think he's wanting Python solution,
So assuming address and num_columns both +ve integer, AND zero-based indexing, maybe just
Code:
row_index = address // num_columns
col_index = address % num_columns
AFTER HERE is just me Waffling
Easier {for me} to do in C or even Avisynth script than in Python [I dont speak Python].
On looking at OP, I was not sure if was written to also cope with -ve address AND array subscript results [end relative or something, seemed overly complicated].
From here[Learn X in Y minutes<Python>]:-
https://learnxinyminutes.com/docs/python/
Quote:
# Math is what you would expect
1 + 1 # => 2
8 - 1 # => 7
10 * 2 # => 20
35 / 5 # => 7.0
# Integer division rounds down for both positive and negative numbers.
5 // 3 # => 1
-5 // 3 # => -2
5.0 // 3.0 # => 1.0 # works on floats too
-5.0 // 3.0 # => -2.0
# The result of division is always a float
10.0 / 3 # => 3.3333333333333335 # EDIT: I think that means also 10 / 3 => 3.3333333333333335
# Modulo operation
7 % 3 # => 1
# i % j have the same sign as j, unlike C
-7 % 3 # => 2
|
EDIT:
Code:
# Avisynth Script
blankclip
Subtitle(String( -7 / 3 ),size=48) # result = -2
Subtitle(String( -7 % 3 ),Y=100,size=48) # result = -1 : ie (-2*3) + -1 => -7
return last
# I think Python -7 // 3 => -3
# I think Python -7 % 3 => 2 : ie, (-3*3) + 2 => -7
EDIT:
Quote:
On looking at OP, I was not sure if was written to also cope with -ve address AND array subscript results [end relative or something, seemed overly complicated].
|
From Python X in Y minutes thingy
Quote:
# li is now [1, 2, 4, 3] again.
# Access a list like you would any array
li[0] # => 1
# Look at the last element
li[-1] # => 3
|