Skip to main content
Code Review

Return to Question

added 100 characters in body; edited tags
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Description of challengechallenge:

Using names.txtnames.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?

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?

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?

Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

Project Euler 22: Names scores

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:

  • 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 FileReader(filePath))) {
 return reader.readLine();
 } catch (IOException e) {
 e.printStackTrace();
 return null;
 }
 }
 /**
 * Gets the value of the given string. If there are characters other than
 * [A-Z], then it will ignore it. Note that the problem file only contains
 * names with all caps.
 * 
 * @param string
 * the name to get the value of
 * @param i
 * the index of the name
 * @return the value of the name
 */
 private static int getValueOfName(String string, int i) {
 char[] array = string.toCharArray();
 int sum = 0;
 for (char c : array) {
 if (Character.isUpperCase(c)) {
 sum += c - ASCII_VALUE_TO_INT;
 }
 }
 return sum * i;
 }
}

Output:

The result is: 871198282
Time taken (ns): 19757146

Time taken seems to be around 20_000_000 nanoseconds.

Specific concerns:

  1. Is there a better way to solve this? (As said above)
  2. Where it says the // naming comment, is the naming fine? Anywhere else where the naming is off?
  3. Anything else?
lang-java

AltStyle によって変換されたページ (->オリジナル) /