Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Project Euler Project 22.
- ParseInput: get the list of names provided into a Python list.
- Sort input list into alpha order
- Iterate through the list totaling up the weighted score for each name.
- NameScore - given a name, compute its score (A=1, B=2, C=3...)
- NameScore is the sum of the value of the letters in the name
- """
- def ParseInput(filename):
- from pyparsing import (delimitedList, QuotedString)
- grammar=delimitedList(QuotedString('"'))
- return list(grammar.parseFile(filename, parseAll=True))
- class NameScorer(object):
- """
- When a NameScorer object is created, it is given an alphabet.
- All the characters in names passed to the score method must contain only
- characters in that alphabet.
- """
- def __init__(self, alphabet):
- self.valuedict=dict(zip(alphabet, range(1,len(alphabet)+1)))
- def score(self,name):
- return sum(self.valuedict[c] for c in name)
- def main():
- nameList=ParseInput("problem22.txt")
- nameList.sort()
- ns=NameScorer("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .'")
- return sum(i*ns.score(name) \
- for (i,name) in zip(range(1,len(nameList)+1), nameList))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement