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, dblQuotedString,
- # Token, ParserElement)
- from pyparsing import (delimitedList, QuotedString)
- # qs=QuotedString('"')
- # print "type(qs)", type(qs)
- # if isinstance(qs, ParserElement):
- # print "qs is an instance of a ParserElement"
- # else:
- # print "qs is not an instance of a ParserElement"
- grammar=delimitedList(QuotedString('"'))
- return(list(grammar.parseFile(filename, parseAll=True)))
- def nameScore(name, valuedict):
- total=0
- for c in list(name):
- total += valuedict[c]
- return total
- def main():
- nameList=ParseInput("problem22.txt")
- # print nameList # test-it
- # for name in nameList:
- # print name
- # print "** Sorting **"
- # print "type(nameList)", type(nameList)
- nameList.sort()
- # print "** Sorted name list **:"
- # i=0
- # for name in nameList:
- # print name
- # if i+1 >= len(nameList):
- # break
- # if nameList[i] > nameList[i+1]:
- # print "**Above name is not in the right place**"
- # i+=1
- valuedict=dict()
- i=1
- for c in list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ."):
- valuedict[c]=i
- i+=1
- #for c in list("ABCabcXYZxyz."): #test-it
- # print c, valuedict[c]
- i=1
- total=0
- for name in nameList:
- total+=i*nameScore(name, valuedict)
- # print name, i, nameScore(name, valuedict) # test-it
- i+=1
- return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement