Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Array searching and scoring?

Sam Bluesman
Ranch Hand
Posts: 191
Suppose I have an array of integers:

int[] numbers = [2,2,1,1,2,2,1,1,2,1]

These integers index originate from another array:

int[] primaryKey = [1, 2]

Now, an ideal situation for me would be if all numbers of the same value are next to each other in the array. e.g.

int[] numbers = [1,1,1,1,1,2,2,2,2,2]

So, in order to determine how 'good' the array is, I need to create a score the array depending on how well they are positioned, using the following rules (assume 'counter' and 'score' begin at 0):

Find first/next position of occurence of primaryKey[counter]

if next integer is the same then score = score + 3
repeat until end of repated integers found

Find next position of occurence of primaryKey[i], until end of array is reached and repeat the above. In which case...

if no more primaryKey[i] is found in array, then increment i to move onto the next element in the primaryKey array and see how badly this next integer under examination is scattered around the array using the rules above.

I have been trying to figure this out but I just can't. Can anyone help me please?
[ February 22, 2006: Message edited by: Sam Bluesman ]

Garrett Rowe
Ranch Hand
Posts: 1296
Just to be sure I understand the question , would the "score" of the "ideal array in your example be 24?

William Barnes
Ranch Hand
Posts: 986
Not sure what why you are adding 3 to the score for a good pair of numbers.

My 2 minute first cut at sudo code

Sam Bluesman
Ranch Hand
Posts: 191
24, that is correct

Sam Bluesman
Ranch Hand
Posts: 191