Created
June 2, 2011 18:12
-
-
Save baversjo/1004934 to your computer and use it in GitHub Desktop.
array desort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<Double> locTeamRatios = new ArrayList<Double>(); | |
Team[] mylist = currentTeams.toArray(new Team[currentTeams.size()]); | |
Team result[] = new Team[mylist.length]; | |
Arrays.sort(mylist,Team.POSTAL_ORDER); | |
for (int y = 0; y < result.length; y++) { | |
result[y] = new Team(-1,"",null,-1); | |
} | |
for (int y = 0; y < mylist.length; y++) { | |
Team el = mylist[y]; | |
int bestIndex = 0; | |
int bestDistance = -1; | |
int bestDiff = -1; | |
for (int i = 0; i < result.length; i++) { | |
Team el2 = result[i]; | |
if(el2.postal > -1){ continue; } | |
Team left[] = Arrays.copyOfRange(result, 0, i); | |
Team right[] = new Team[0]; | |
if(!(i+1 > result.length -1)){ | |
right = Arrays.copyOfRange(result, i+1,result.length); | |
} | |
int leftRes = 1; | |
int rightRes = 1; | |
int leftDiff = 0; | |
int rightDiff = 0; | |
//reverse left | |
List <Team> list = Arrays.asList(left); | |
Collections.reverse(list); | |
left = list.toArray(new Team[list.size()]); | |
for (int j = 0; j < left.length; j++) { | |
Team el3 = left[j]; | |
if(el3.postal != -1){ | |
leftRes = j+1; | |
leftDiff = (int) Math.abs(el3.postal - el.postal); | |
break; | |
} | |
} | |
for (int j = 0; j < right.length; j++) { | |
Team el3 = right[j]; | |
if(el3.postal != -1){ | |
rightRes = j+1; | |
rightDiff = (int) Math.abs(el3.postal - el.postal); | |
break; | |
} | |
} | |
int total = leftRes * rightRes; | |
int closestDiff = rightDiff; | |
if(leftRes < rightRes){ | |
closestDiff = leftRes; | |
} | |
if(total > bestDistance || (total == bestDistance && closestDiff > bestDiff)){ | |
bestDistance = total; | |
bestIndex = i; | |
bestDiff = closestDiff; | |
} | |
} | |
result[bestIndex] = el; | |
} | |
return result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mylist = [0,0,0,5,8,3,0,2,5,9,5,0,5,10,6,3,5,3,0,0,5,3] #test data | |
result = [] | |
mylist = sorted(mylist) | |
for el in mylist: result.append(-1) | |
for y,el in enumerate(mylist): | |
bestIndex = 0 | |
bestDistance = -1 | |
bestDiff = -1 | |
for i,el2 in enumerate(result): | |
if el2 > -1: continue | |
left = result[0:i] | |
right = result[i+1:] | |
leftRes = 1 | |
rightRes = 1 | |
leftDiff = 0 | |
rightDiff = 0 | |
left.reverse() | |
for j,el3 in enumerate(left): | |
if el3 != -1: | |
leftRes = j+1; | |
leftDiff = abs(el3-el) | |
break; | |
for j,el3 in enumerate(right): | |
if el3 != -1: | |
rightRes = j+1; | |
rightDiff = abs(el3-el) | |
break; | |
total = leftRes * rightRes | |
closestDiff = rightDiff if leftRes > rightRes else leftDiff | |
if total > bestDistance or (total == bestDistance and closestDiff > bestDiff): | |
bestDistance = total | |
bestIndex = i | |
bestDiff = closestDiff | |
result[bestIndex] = el | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment