John Lekberg


JUNTO Practice - "Project Euler Problem 22"

Discussed on 2018-08-20.

Project Euler Problem 22


Solution - John

#!/usr/bin/env python3

with open("names.txt", "r") as namefile:
    content = namefile.read().replace('"', "")
    names = sorted(content.split(","))
    total = 0

    for i, name in enumerate(names, start=1):
        alpha_val = sum(
            map(lambda c: 1 + ord(c) - ord("A"), name)
        )
        total += i * alpha_val

    print(total)

Solution - Oscar

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 18 18:50:36 2018

@author: oscarmartinez
"""

#%%

"""Set Working Directory, change to your needs."""

import os

os.getcwd()
os.chdir("/Users/oscarmartinez/Master Files/JUNTO")

#%%

"""Read off the names, remove the quotation marks and split on commas"""
"""Sort the list in place as specified by problem"""

strNamesList = open("p022_names.txt").read().replace('"', "")
lstNames = strNamesList.split(sep=",")
lstNames.sort()

#%%

"""create Dict to map strings"""
"""NOTE: could use method .maketrans() and then str.translate() instead
in order to create a way to map name scores"""

import string

dictCharScore = {}
for i, char in enumerate(string.ascii_uppercase):
    dictCharScore[char] = i + 1

#%%

"""manual sum over the list, could also use a list of numbers and then
sum over the list"""

fltTotalScore = 0.0
for row, name in enumerate(lstNames):
    iBaseScore = 0
    for char in name:
        iBaseScore += dictCharScore[char]

    iScore = iBaseScore * (row + 1)
    fltTotalScore += iScore

print(fltTotalScore)