patternjavaMinor
Project Euler 22: Names scores
Viewed 0 times
projecteulerscoresnames
Problem
Description of challenge:
Using names.txt (right click and 'Save Link/Target As...'), a 46K text
file containing over five-thousand first names, begin by sorting it
into alphabetical order. Then working out the alphabetical value for
each name, multiply this value by its alphabetical position in the
list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN,
which is worth \$3 + 15 + 12 + 9 + 14 = 53\$, is the 938th name in the
list. So, COLIN would obtain a score of \$938 × 53 = 49714\$.
What is the total of all the name scores in the file?
I quickly created a solution: it was fairly easy, but I am specifically concerned about the performance, which I am unsure of. What I do know is:
The code is below:
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class ProjectEuler22 {
private static final String filePath = "names.txt";
private static final int ASCII_VALUE_TO_INT = 64; // naming
public static void main(String[] args) {
long time = System.nanoTime();
String[] names = readAllNames().split("\",\"");
names[0] = names[0].substring(1);
int length = names.length; // extra "
names[length - 1] = names[length - 1].substring(0,
names[length - 1].length()); // extra "
Arrays.sort(names);
int total = 0;
for (int i = 0; i < length; i++) {
total += getValueOfName(names[i], i + 1);
}
time = System.nanoTime() - time;
System.out.println("The result is: " + total + "\nTime taken (ns): "
+ time);
}
private static String readAllNames() {
try (BufferedReader reader = new BufferedReader(
new
Using names.txt (right click and 'Save Link/Target As...'), a 46K text
file containing over five-thousand first names, begin by sorting it
into alphabetical order. Then working out the alphabetical value for
each name, multiply this value by its alphabetical position in the
list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN,
which is worth \$3 + 15 + 12 + 9 + 14 = 53\$, is the 938th name in the
list. So, COLIN would obtain a score of \$938 × 53 = 49714\$.
What is the total of all the name scores in the file?
I quickly created a solution: it was fairly easy, but I am specifically concerned about the performance, which I am unsure of. What I do know is:
string.split(): \$O(n)\$
Arrays.sort(): \$O(n \log n)\$ Worst Case
- Remaining code: \$O(nm)\$, where \$n\$ is the length, and \$m\$ is the average length of the names
The code is below:
```
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class ProjectEuler22 {
private static final String filePath = "names.txt";
private static final int ASCII_VALUE_TO_INT = 64; // naming
public static void main(String[] args) {
long time = System.nanoTime();
String[] names = readAllNames().split("\",\"");
names[0] = names[0].substring(1);
int length = names.length; // extra "
names[length - 1] = names[length - 1].substring(0,
names[length - 1].length()); // extra "
Arrays.sort(names);
int total = 0;
for (int i = 0; i < length; i++) {
total += getValueOfName(names[i], i + 1);
}
time = System.nanoTime() - time;
System.out.println("The result is: " + total + "\nTime taken (ns): "
+ time);
}
private static String readAllNames() {
try (BufferedReader reader = new BufferedReader(
new
Solution
-
Well, there's a potentially more efficient way:
Read the whole file into a byte-array (It's not even a measly 64K), thus avoiding any conversion overhead and most object-allocations, extract the start-indices of all the names, and sort them.
Is it equally nice? Probably not. And it will only speed it up by a constant factor, because more just isn't there to be had.
Anyway, measure it because while going down a level gives opportunities for optimization, it's easy to get it wrong.
-
If you don't want to go quite that far, here's an easy one: Strip the trailing
-
Getting to your constants name, that's atrocious. Because, you know, using a named constant for that at all is so bad.
You can simply use a character-literal:
-
You fail to close the file in
Also, why do you catch
Well, there's a potentially more efficient way:
Read the whole file into a byte-array (It's not even a measly 64K), thus avoiding any conversion overhead and most object-allocations, extract the start-indices of all the names, and sort them.
Is it equally nice? Probably not. And it will only speed it up by a constant factor, because more just isn't there to be had.
Anyway, measure it because while going down a level gives opportunities for optimization, it's easy to get it wrong.
-
If you don't want to go quite that far, here's an easy one: Strip the trailing
" from the last name, and you can drop the test whether you actually have a letter in getValueOfName.-
Getting to your constants name, that's atrocious. Because, you know, using a named constant for that at all is so bad.
You can simply use a character-literal:
'A'-1-
You fail to close the file in
readAllNames if newing the BufferedReader fails. Admittedly that's unlikely in such a small program, but anyway.Also, why do you catch
IOException there, but then return null which immediately causes a NPE in turn in main?Context
StackExchange Code Review Q#110411, answer score: 5
Revisions (0)
No revisions yet.