Home > Back-end >  Project Euler #22 Names Scores - Python
Project Euler #22 Names Scores - Python

Time:12-17

I'm struggling to find the logical error in my code. So I'm kindly asking you to help me find the 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?

Thank all of you for pointing out to the part overlooked by me. Already edited. The answer it gives me is 871179673, whereas the correct answer is 871198282

import string

alphabet = string.ascii_uppercase
alpha_d = {}
letter_score, total_score = 0, 0


for ind, let in enumerate(alphabet, start=1):  # Assigning all letters and their indexes to the dictionary
    alpha_d[let] = ind


with open("name.txt", 'r') as f:
    lines = f.readlines()
    lines = str(lines).split(",")  # Opening the file and assigning the string to "lines"
    lines = sorted(lines)


for index, line in enumerate(lines, start=1):  # This 'for' goes through all the names
    line = line.upper()
    letter_score = 0

    for letter in list(line):  # This 'for' goes through the letter in the name
        if letter in alpha_d:
            letter_score  = alpha_d[letter]  # If the letter is in the dictionary, it's score will be added

    total_score  = (letter_score * index)  # The final score will be added to 'total score'

CodePudding user response:

Try:

# read your file and sort it
lines = sorted(eval(open('names.txt').read()))

total_score = 0
for index, line in enumerate(lines, start=1):
    total_score  = index * sum(ord(c) - 64 for c in line)

# Alternative with alphabet
total_score = 0
for index, line in enumerate(lines, start=1):
    total_score  = index * sum(alphabet.index(c) 1 for c in line)

Output:

>>> total_score
871198282

Without sort file, the total_score is 850081394.

Source: https://projecteuler.net/project/resources/p022_names.txt

CodePudding user response:

"Begin by sorting it into alphabetical order" It doesn't seem you have!

CodePudding user response:

I suspect that the difference comes from the order of the names. I don't see where you sorted the list before iterating through the names.

CodePudding user response:

Just start by sorting it and then go on to the rest of your program. You can add index, and enumerate and then calculate the value of each line.

with open("name.txt", 'r') as f:
    for line in sorted(f):

CodePudding user response:

Well it only works for sorted arrays as the index of the word will be different in an unsorted array, which will make the total_score wrong.

Also, in the line lines = str(lines).split(",") of your code, the str(lines) considers the brackets of list too i.e [], which you managed by adding an if to check if the letter from the string is in the dictionary of alphabets. Now without that if it would work much faster. So you modify the string to its needs by stripping it off of ", [, ,, ].

One more suggestion that which I would like to suggest is that, you can use f.read() to read the entire file as a string which will be even more helpful as you can remove the unwanted things and get the names alone... Like this

from string import ascii_uppercase as upper

total_score = 0

with open("names.txt") as f:
    data = f.read().split(",") # Removes the comma, so as to get names along with quotes

L1 = sorted([_.strip('"') for _ in data]) # Removes the individual quotes and sorts the list as well

for i in L1:
    letter_score = sum([upper.index(j) 1 for j in i]) # Sums up the index of individual letters in the words so as to get letter score

    total_score  = letter_score*(L1.index(i) 1)

print(total_score)
  • Related