Names Score — Euler Project

Reid Jackson
2 min readJan 20, 2022

I’ve continued my trek towards learning python and this week I branched out into python modules. Its a good thing I’m just learning about modules, because they are infinitely easier than the Ruby gem system. In short, a module imports a library of code beyond vanilla python. For my practice this week, I needed to access a dictionary and CSV parsing functionality. Here’s the problem that pushed me into modules:

To get a sense of what names.txt looks like, here’s a peek:

The first order of business is importing this file into a list that I can work with.

‘import csv’ gave me access to the open() function. This function parses through the file and I was able to store that data as a variable —names_array which I sorted straight away. Before moving forward, I created a function that could score an individual name given two arguments. The first argument is the name itself and the second argument is an index value. In this function letters earn a score based on a dictionary that I created using the string library. In this case string.ascii_uppercase returns an uppercase alphabet that I converted to a list. That list was translated again into a dictionary so that each letter key could be assigned a value — one through twenty-six. The name scoring function is ready to go.

From here, I simply iterated through each name on the list. Each name score is added to the total and, voila we reach our solution. One hiccup that makes me miss Ruby a little is the each_with_index function. There doesn’t seem to be an equivalent in Python, but the workaround of incrementing a variable, in this case ‘J’ isn’t the worst thing in the world.

--

--