/**
* 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?
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
*
* @author Umang B Bhatt bhatt.umang7@gmail.com
*/
public class Problem_22
{
/**
* name of file in which names are stored
*/
public static final String fileName = "names.txt";
/**
*
* @return the content of file as string
* @throws FileNotFoundException
*/
public static String getFileContent() throws FileNotFoundException
{
String data = "";
FileInputStream fis = new FileInputStream(fileName);
Scanner scanner = new Scanner(fis);
while (scanner.hasNextLine())
{
data += scanner.nextLine();
}
return data;
}
/**
*
* @param fileContent is the content of file
* @return returns a linked list of string containing names that were
* extracted from file content.
*/
public static LinkedList<String> getNamesFromFileData(String fileContent)
{
LinkedList<String> nameList = new LinkedList<>();
StringTokenizer strTOK = new StringTokenizer(fileContent, "\",");
while (strTOK.hasMoreTokens())
{
nameList.add(strTOK.nextToken());
}
return nameList;
}
/**
*
* @param name is the string for which sum is to be calculated
* @return sum of values of string.
*/
public static int getSumOfCharsInString(String name)
{
int sum = 0;
for (int i = 0; i < name.length(); i++)
{
sum += getIntValueForChar(name.charAt(i));
}
return sum;
}
/**
*
* @param c is the char for which value is to be calculated
* @return the value for char. eg. 1 for A, 2 for B and so on.
*/
public static int getIntValueForChar(char c)
{
int value = (int) c;
value -= 64; // 65 for A and so on
return value;
}
public static void main(String args[]) throws FileNotFoundException
{
//get the names in name list
String content = getFileContent();
LinkedList<String> nameList = getNamesFromFileData(content);
// sorth the list
Collections.sort(nameList);
BigInteger score = new BigInteger("0");
// sum up the score
for (int i = 0; i < nameList.size(); i++)
{
Integer j = (i + 1) * getSumOfCharsInString(nameList.get(i));
score = score.add(new BigInteger(j.toString()));
}
System.out.println("score is : " + score);
}
}
Tuesday, June 26, 2012
total of all the name scores in the file
Labels:
file,
java,
names score in file. eueler,
problem 22,
umang bhatt
Subscribe to:
Post Comments (Atom)
this program was written in 25 minutes
ReplyDeletethis code is good.
ReplyDelete