Created
August 10, 2012 00:14
-
-
Save JossWhittle/3309401 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public void p22() { | |
String[] v = {"MARY","PATRICIA","LINDA","BARBARA",...}; | |
v = merge(v); | |
long sum = 0; | |
for (int i = 0; i < v.length; i++) { | |
sum += scoreWord(v[i]) * (i + 1); | |
} | |
System.out.println(sum); | |
} | |
public String[] merge(String array[]) { | |
if (array.length > 1) { | |
int elementsInA1 = array.length/2; | |
int elementsInA2 = elementsInA1; | |
if ((array.length % 2) == 1) { | |
elementsInA2 += 1; | |
} | |
String arr1[] = new String[elementsInA1]; | |
String arr2[] = new String[elementsInA2]; | |
for (int i = 0; i < elementsInA1; i++) { | |
arr1[i] = array[i]; | |
} | |
for (int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) { | |
arr2[i - elementsInA1] = array[i]; | |
} | |
arr1 = merge(arr1); | |
arr2 = merge(arr2); | |
int i = 0, j = 0, k = 0; | |
while (arr1.length != j && arr2.length != k) { | |
if (arr1[j].compareTo(arr2[k]) < 0) { | |
array[i] = arr1[j]; | |
i++; | |
j++; | |
} else { | |
array[i] = arr2[k]; | |
i++; | |
k++; | |
} | |
} | |
while(arr1.length != j) { | |
array[i] = arr1[j]; | |
i++; | |
j++; | |
} | |
while(arr2.length != k) { | |
array[i] = arr2[k]; | |
i++; | |
k++; | |
} | |
} | |
return array; | |
} | |
public int scoreWord(String word) { | |
char[] c = {'A','B','C','D','E','F','G','H','I','J','K',...}; | |
int s = 0; | |
for (int i = 0; i < word.length(); i++) { | |
for (int j = 0; j < c.length; j++) { | |
if (c[j] == word.charAt(i)) { | |
s += (j + 1); | |
j = c.length; | |
} | |
} | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment