Names scores : Problem 22 : Project Euler
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?
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?
JAVA CODE
import java.io.*;
import java.util.*;
public class NamesScore {
public static void main(String[] args) throws IOException {
String fileName = "names.txt";
String allNames = "";
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
allNames=br.readLine();
br.close();
fr.close();
NameExtractionScores nms = new NameExtractionScores();
System.out.println("Total number of names: "+nms.namesCount(allNames));
System.out.println("LIST of the names (unsorted):");
nms.namesExtraction(allNames);
System.out.println("Score is: "+nms.namesScore());
}
}
class NameExtractionScores {
public int namesCount(String allNames) {
for (int i = 0; i < allNames.length(); i++) {
if (allNames.charAt(i) == ',')
nameCount++;
}
return nameCount;
}
public void namesExtraction (String allNames) {
names = new String[nameCount];
String temp = "";
int k = 0;
for (int i = 0; i < allNames.length(); i++) {
temp = "";
while ( allNames.charAt(i) != ',' ) {
if (allNames.charAt(i) != '"')
temp += allNames.charAt(i++);
else
i++;
}
names[k] = "";
names[k] +=temp;
System.out.println(names[k++]);
}
}
public int namesScore() {
Arrays.sort(names);
for (int i = 0; i < nameCount; i++) {
int pSum = 0;
System.out.println(names[i]);
for (int j = 0; j < names[i].length(); j++)
pSum += (names[i].charAt(j)-64);
nameScoreTotal += (pSum*(i+1));
}
return nameScoreTotal;
}
private int nameScoreTotal = 0;
private int nameCount = 0;
private String[] names;
}
import java.util.*;
public class NamesScore {
public static void main(String[] args) throws IOException {
String fileName = "names.txt";
String allNames = "";
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
allNames=br.readLine();
br.close();
fr.close();
NameExtractionScores nms = new NameExtractionScores();
System.out.println("Total number of names: "+nms.namesCount(allNames));
System.out.println("LIST of the names (unsorted):");
nms.namesExtraction(allNames);
System.out.println("Score is: "+nms.namesScore());
}
}
class NameExtractionScores {
public int namesCount(String allNames) {
for (int i = 0; i < allNames.length(); i++) {
if (allNames.charAt(i) == ',')
nameCount++;
}
return nameCount;
}
public void namesExtraction (String allNames) {
names = new String[nameCount];
String temp = "";
int k = 0;
for (int i = 0; i < allNames.length(); i++) {
temp = "";
while ( allNames.charAt(i) != ',' ) {
if (allNames.charAt(i) != '"')
temp += allNames.charAt(i++);
else
i++;
}
names[k] = "";
names[k] +=temp;
System.out.println(names[k++]);
}
}
public int namesScore() {
Arrays.sort(names);
for (int i = 0; i < nameCount; i++) {
int pSum = 0;
System.out.println(names[i]);
for (int j = 0; j < names[i].length(); j++)
pSum += (names[i].charAt(j)-64);
nameScoreTotal += (pSum*(i+1));
}
return nameScoreTotal;
}
private int nameScoreTotal = 0;
private int nameCount = 0;
private String[] names;
}
No comments:
Post a Comment