Project Euler – Problem # 22 – Solved with Go

What is the total of all the name scores in the file of first names?

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?

**********

To solve this problem, I had help with reading the file and making it ready for the program from a stackoverflow question I asked – http://stackoverflow.com/questions/11462879/strings-split-in-golang

One Possible Solution: Go

package main

import "fmt"
import "io/ioutil"
import "strings"
import "sort"

func main() {
	fData, err := ioutil.ReadFile("names.txt")	// read in the external file
	if err != nil {
		fmt.Println("Err is ", err)	// print any error
	}
	strbuffer := string(fData)	// convert read in file to a string

	names := strings.Split(strbuffer, ",")

	for i, val := range names {
		names[i] = strings.Trim(val, "\"")
	}

	sort.Strings(names)

	total := 0

	for i := range names {
		name := names[i]
		letters := strings.Split(name, "")

		x2 := 0
		y := 0

		for m := range letters {
			letter := letters[m]
			x1 := letter_counter(letter)
			x2 += x1
		}
		y = x2 * (i + 1)
		total += y

	}
	fmt.Println(total)

}

func letter_counter(letter string) (lc int) {
	letv := make(map[string]int)

	letv["A"] = 1
	letv["B"] = 2
	letv["C"] = 3
	letv["D"] = 4
	letv["E"] = 5
	letv["F"] = 6
	letv["G"] = 7
	letv["H"] = 8
	letv["I"] = 9
	letv["J"] = 10
	letv["K"] = 11
	letv["L"] = 12
	letv["M"] = 13
	letv["N"] = 14
	letv["O"] = 15
	letv["P"] = 16
	letv["Q"] = 17
	letv["R"] = 18
	letv["S"] = 19
	letv["T"] = 20
	letv["U"] = 21
	letv["V"] = 22
	letv["W"] = 23
	letv["X"] = 24
	letv["Y"] = 25
	letv["Z"] = 26

	lc = letv[letter]

	return lc
}
Advertisements

Project Euler – Problem # 22 – Solved with Java

What is the total of all the name scores in the file of first names?

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?

**********

To solve this problem, I had help with reading the file and making it ready for the program from a stackoverflow question/answerhttp://stackoverflow.com/questions/8672712/optimizing-project-euler-22

One Possible Solution: Java

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.File;

public class Problem_22 {
	public static void main(String args[]){
		try
	    {
	       Scanner in = new Scanner(new File("names.txt")).useDelimiter("[\",]+");
	       ArrayList<String> names = new ArrayList<String>();
	       while (in.hasNext())
	       {
	    	   names.add(in.next());
	       }
	       Collections.sort(names);
	       
	       int z = 0;
	       for(int i = 0; i <= names.size() - 1; i ++){
	    	   int x2 = 0;
	    	   int y = 0;
	    	   String name = names.get(i);
	    	   for(int m = 0; m <= name.length() - 1; m ++){
	    		   int x1 = letter_counter(name.charAt(m));
	    		   x2 += x1;
	    	   }
	    	   y = x2 * (i + 1);
	    	   z += y;
			}
	       System.out.format("Total = %d%n", z);
	    }
		catch( FileNotFoundException filenotfoundexcption )
		{
		   System.out.println( "names.txt, does not exist" );
		}
		 
		catch( IOException ioexception )
		{
		   ioexception.printStackTrace( );
		}
	}
	
	public static Integer letter_counter(char letter_u)
	{
		String letter = Character.toString(letter_u);
		Map<String, Integer> letv = new HashMap<String, Integer>();
		letv.put("A", 1);
		letv.put("B", 2);
		letv.put("C", 3);
		letv.put("D", 4);
		letv.put("E", 5);
		letv.put("F", 6);
		letv.put("G", 7);
		letv.put("H", 8);
		letv.put("I", 9);
		letv.put("J", 10);
		letv.put("K", 11);
		letv.put("L", 12);
		letv.put("M", 13);
		letv.put("N", 14);
		letv.put("O", 15);
		letv.put("P", 16);
		letv.put("Q", 17);
		letv.put("R", 18);
		letv.put("S", 19);
		letv.put("T", 20);
		letv.put("U", 21);
		letv.put("V", 22);
		letv.put("W", 23);
		letv.put("X", 24);
		letv.put("Y", 25);
		letv.put("Z", 26);
		
		int lc = letv.get(letter);
		return lc;
	}
}

Project Euler – Problem # 22 – Solved with Python

The hardest part about this problem was reading the file (names.txt) into the program for proper file manipulation.

Problem:

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?

One Possible Solution:

f = open('names.txt', 'rU')
names = []

names = sorted(f.read().replace('"', '').split(','), key=str)

letv = {'A':1,'B':2,'C':3,'D':4,'E':5,'F':6,'G':7,'H':8,'I':9,'J':10, \
        'K':11,'L':12,'M':13,'N':14,'O':15,'P':16,'Q':17,'R':18,'S':19, \
        'T':20,'U':21,'V':22,'W':23,'X':24,'Y':25,'Z':26}

def letter_counter(letter):
    """Letter counter"""
    lc = letv[letter]
    return lc
   
def main():
    """Main program"""
    x = len(names)
    z = 0
    for i in range(0,(x)):
        x2 = 0
        y = 0
        for letter in names[i]:
            x1 = letter_counter(letter)
            x2 = x2 + x1
        y = x2 * (i + 1)
        z = z + y
    print "z = ", z
            
if __name__ == '__main__':
    main()