• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
21%
SHARE
TWEET

# Problem 22

rdrewd Mar 19th, 2013 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """
2.    Project Euler Project 22.
3.        ParseInput: get the list of names provided into a Python list.
4.        Sort input list into alpha order
5.        Iterate through the list totaling up the weighted score for each name.
6.        NameScore - given a name, compute its score (A=1, B=2, C=3...)
7.           NameScore is the sum of the value of the letters in the name
8.    """
9.
10. def ParseInput(filename):
11.     from pyparsing import (delimitedList, QuotedString)
12.     grammar=delimitedList(QuotedString('"'))
13.     return list(grammar.parseFile(filename, parseAll=True))
14. class NameScorer(object):
15.     """
16.        When a NameScorer object is created, it is given an alphabet.
17.        All the characters in names passed to the score method must contain only
18.            characters in that alphabet.
19.    """
20.     def __init__(self, alphabet):
21.         self.valuedict=dict(zip(alphabet, range(1,len(alphabet)+1)))
22.     def score(self,name):
23.         return sum(self.valuedict[c] for c in name)
24.
25. def main():
26.     nameList=ParseInput("problem22.txt")
27.     nameList.sort()
28.     ns=NameScorer("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .'")
29.     return sum(i*ns.score(name) \
30.         for (i,name) in zip(range(1,len(nameList)+1), nameList))
RAW Paste Data
Top