Project Euler #22: Names scores

Sort by

recency

|

96 Discussions

|

  • + 0 comments

    100% python code

    n = int(input())
    names = list(sorted([input() for _ in range(n)]))
    q = int(input())
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for _ in range(q):
        name = input()
        value = 0
        for letter in name:
            value += alphabet.index(letter) + 1
        value *= (names.index(name) + 1)
        print(value)
    
  • + 0 comments

    // Using java and all test case passed

    import java.util.*;

    public class Solution {

    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        // Key pair value for alphabets 
        Map<String, Integer> map = new HashMap<>();
        map.put("A", 1); map.put("H", 8); map.put("O", 15);  map.put("V", 22);
        map.put("B", 2); map.put("I", 9); map.put("P", 16);  map.put("W", 23);
        map.put("C", 3); map.put("J", 10); map.put("Q", 17);  map.put("X", 24);
        map.put("D", 4); map.put("K", 11); map.put("R", 18);  map.put("Y", 25);
        map.put("E", 5); map.put("L", 12); map.put("S", 19);  map.put("Z", 26);
        map.put("F", 6); map.put("M", 13); map.put("T", 20);  
        map.put("G", 7); map.put("N", 14); map.put("U", 21);
    
        // generate list and sort it correspondingly 
    
        int n = sc.nextInt();
        ArrayList <String> lst = new ArrayList<String>();
        for(int i=0;i<n ; i++){
            String str = sc.next();
            lst.add(str);
        }
        Collections.sort(lst);
    
        // check for a string in list with its position and calculate the logic 
    
        int q = sc.nextInt();
        for(int i=0;i<q ; i++){
            int sum = 0;
            String str = sc.next();
            int pos = lst.indexOf(str);
            for(int j=0;j<str.length();j++){
                String c = String.valueOf(str.charAt(j));
                int num = map.get(c);
                sum += num ;
            }
            sum = sum*(pos+1);
            System.out.println(sum);
        }
    }
    

    }

  • + 0 comments

    //C# using System; using System.Collections.Generic;

    class Solution { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str = new string[n];

    SortedDictionary<string, int> sorted = new SortedDictionary<string, int>();
    
    for (int i = 0; i < n; i++)
    {
        int sum = 0;
    
        str[i] = Console.ReadLine();
        char[] characters = str[i].ToCharArray();
        foreach (char ch in characters)
        {
            sum += (int)ch - 64; 
        }
        sorted[str[i].ToUpper()] = sum;
    }
    
    var entryArray = new KeyValuePair<string, int>[sorted.Count];
    sorted.CopyTo(entryArray, 0);
    
    for (int i = 0; i < sorted.Count; i++)
    {
        int newNumber = entryArray[i].Value * (i + 1);
        sorted[entryArray[i].Key] = newNumber;
    }
    
    int q = int.Parse(Console.ReadLine());
    
    for (int i = 0; i < q; i++)
    {
        string name = Console.ReadLine();
        if (sorted.ContainsKey(name.ToUpper()))
        {
            Console.WriteLine(sorted[name.ToUpper()]);
        }
    }
    

    } }

  • + 0 comments

    //c# code using System; using System.Collections.Generic;

    class Solution { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str = new string[n];

        SortedDictionary<string, int> sorted = new SortedDictionary<string, int>();
    
        for (int i = 0; i < n; i++)
        {
            int sum = 0;
    
            str[i] = Console.ReadLine();
            char[] characters = str[i].ToCharArray();
            foreach (char ch in characters)
            {
                sum += (int)ch - 64; // - 64 because the ASCII value of 'A' is 65, so 'A' - 64 = 1
            }
            sorted[str[i].ToUpper()] = sum;
        }
    
        // to get the key and value of SortedDictionary using Index
        var entryArray = new KeyValuePair<string, int>[sorted.Count];
        sorted.CopyTo(entryArray, 0);
    
        // updating the assigned value to names after sorted position. This is why case 1 fails
        for (int i = 0; i < sorted.Count; i++)
        {
            int newNumber = entryArray[i].Value * (i + 1);
            sorted[entryArray[i].Key] = newNumber;
        }
    
        int q = int.Parse(Console.ReadLine());
    
        for (int i = 0; i < q; i++)
        {
            string name = Console.ReadLine();
            if (sorted.ContainsKey(name.ToUpper()))
            {
                Console.WriteLine(sorted[name.ToUpper()]);
            }
        }
    }
    

    }

  • + 0 comments

    using System; using System.Collections.Generic;

    class Solution { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] str = new string[n];

        SortedDictionary<string, int> sorted = new SortedDictionary<string, int>();
    
        for (int i = 0; i < n; i++)
        {
            int sum = 0;
    
            str[i] = Console.ReadLine();
            char[] characters = str[i].ToCharArray();
            foreach (char ch in characters)
            {
                sum += (int)ch - 64; // - 64 because the ASCII value of 'A' is 65, so 'A' - 64 = 1
            }
            sorted[str[i].ToUpper()] = sum;
        }
    
        // to get the key and value of SortedDictionary using Index
        var entryArray = new KeyValuePair<string, int>[sorted.Count];
        sorted.CopyTo(entryArray, 0);
    
        // updating the assigned value to names after sorted position. This is why case 1 fails
        for (int i = 0; i < sorted.Count; i++)
        {
            int newNumber = entryArray[i].Value * (i + 1);
            sorted[entryArray[i].Key] = newNumber;
        }
    
        int q = int.Parse(Console.ReadLine());
    
        for (int i = 0; i < q; i++)
        {
            string name = Console.ReadLine();
            if (sorted.ContainsKey(name.ToUpper()))
            {
                Console.WriteLine(sorted[name.ToUpper()]);
            }
        }
    }
    

    }