Project Euler #22: Names scores

Question
Using names.txt , 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?

Answer : 871198282

Hacker Rank Problem

Solution

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.Arrays;

public class Solution {

    //Constant alphabet array. Will be used to determine a letter score based on index.
    protected static final char[] ALPHABET_ENG = "abcdefghijklmnopqrstuvwxyz".toCharArray();

    //String array will temporarily hold names for sorting.
    //ArrayList will hold the sorted names.
    protected static String[] namesToBeSorted;
    protected static ArrayList<String> listOfNames;

    /**
     * Main method receives user string inputs (names of people).
     * It then calls method 'calculateNameScore' in order to get the score of each name.
     *
     * @param String[] args NULL
     **/
    public static void main(String[] args) {

        //scanner creation.
        Scanner scan = new Scanner(System.in);

        //integer value of amount of names to be added.
        int amountOfNames = scan.nextInt();

        //temporary names array will hold all names provided for sorting.
        namesToBeSorted = new String[amountOfNames];

        //names added to array.
        for (int index = 0; index < namesToBeSorted.length; index++) {

            //each name is added.
            String name = scan.next();
            namesToBeSorted[index] = name;
        }

        //names are sorted in alphabetical order and are transferred over to ArrayList.
        Arrays.sort(namesToBeSorted);
        listOfNames = new ArrayList<String>(Arrays.asList(namesToBeSorted));

        //integer for number of names to be scored.
        int queries = scan.nextInt();

        //loop runs until each name is scored.
        while (queries-- > 0) {

            //name is grabbed and scoring method is called.
            String nameToBeScored = scan.next();
            System.out.println(calculateNameScore(nameToBeScored));
        }
    }

    /**
     * Method calculates the score of a string.
     *
     * By using the index of the alphabet (1 throught 26), each letter of a given string
     * is indexed with the alphabet. Then the index of each letter in the alphabet is
     * added up and multiplied by the string location (index + 1) in the ArrayList.
     *
     * @param String givenName String to be scored.
     * @return int nameScore Score of the string.
     **/
    protected static int calculateNameScore(final String givenName) {

        //integer variable for the score of a given name.
        int nameScore = 0;

        //character array for each name and location in ArrayList.
        char[] nameScoringArray = givenName.toCharArray();
        int namePlace = listOfNames.indexOf(givenName) + 1;

        //loop runs until each letter is scored and added up.
        for (int index = 0; index < nameScoringArray.length; index++) {

            //each letter is scored.
            nameScore += findCharacterIndex(ALPHABET_ENG, Character.toLowerCase(nameScoringArray[index])) + 1;
        }

        //name score is multiplied with location in ArrayList and returned.
        return nameScore * namePlace;
    }

    /**
     * Method find the index of a character in an character array.
     *
     *
     * @param char[] array Character array to be searched.
     * @return char character The character to be searched for.
     **/
    protected static int findCharacterIndex(char[] array, char character) {

        //loop runs until index of the given character is found.
        for (int index = 0; index < array.length; index++)

            //character index returned if found.
            if (character == array[index]) { return index; }

        //if not found.
        return -1;
    }
}

Comments

Popular Posts