I have the following elements in a list/array
a1,a2,a3
and these elements are used to build another list in a predictable pattern
example
a1,a1,a2,a2,a3,a3,a1,a1,a2,a2,a3,a3...
The pattern may change but I will always know how many times each element repeats and all elements repeats the same number of times. And the elements always show up in the same order.
so another pattern might be
a1,a1,a1,a2,a2,a2,a3,a3,a3,a1,a1,a1,a2,a2,a2,a3,a3,a3...
or
a1,a2,a3,a1,a2,a3
it will never be
a2,a2,a1,a1,a3,a3... or a1,a2,a3,a2,a3,a1 etc
How I determine what element is at any index in the list?
I can't run through the generated list because it is what might be. It doesn't actually exist. And I need to get tbe answer for any index from 0 t开发者_如何学Co infinity (actually integer.maxvalue)
Lets make some denotations:
n - number of elements in orginal array
k - how many times element is repeated
x- index
Array[x] == Array[(x mod (kn)) div k] - that is what You were searching for.
In other words element at index x is equal to element at index (x mod (kn)) div k
Run through the list until you find an element which is not the same as the first element. After that you know how long each group is and you can determine the element at any index with a simple modulo statement.
Pseudo stuff:
determine(index){
firstelement = list[0]
i=0;
for i=0; element.count; i++
if element != firstelement
break;
m = index modulo (i*3)
switch(m)
case 0: return 'a1'
case 1: return 'a2'
case 2: return 'a3'
}
精彩评论