From 8dac6fea28199e422eb3e065969116afa4b31c8d Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 17 Jan 2012 11:00:12 +0100 Subject: [PATCH] Solves Problem 22. Works either on problem 17 or 18. --- e_17.py | 26 ++++++++++++++++++++++++++ e_18.py | 2 +- e_22.py | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100755 e_17.py diff --git a/e_17.py b/e_17.py new file mode 100755 index 0000000..8cbe1a6 --- /dev/null +++ b/e_17.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +""" + ##--- + # Julien Lengrand-Lambert + #Created on : Tue Jan 17 10:57:41 CET 2012 + # + # DESCRIPTION : Solves problem 17 of Project Euler + If the numbers 1 to 5 are written out in words: one, two, three, four, five, + then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. + If all the numbers from 1 to 1000 (one thousand) inclusive were written out in + words, how many letters would be used? + + NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and + forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 + letters. The use of "and" when writing out numbers is in compliance with + British usage. + ##--- +""" +def fun(): + """ + """ + + return 1 + +if __name__ == '__main__': + print "Answer : %d" % (fun()) diff --git a/e_18.py b/e_18.py index fe3042d..774b33a 100755 --- a/e_18.py +++ b/e_18.py @@ -1,7 +1,7 @@ #!/usr/bin/env python """ ##--- - # jlengrand + # Julien Lengrand-Lambert #Created on : Mon Jan 16 15:34:46 CET 2012 # # DESCRIPTION : Solves problem 18 of Project Euler diff --git a/e_22.py b/e_22.py index 37e78cf..1e8cd64 100755 --- a/e_22.py +++ b/e_22.py @@ -11,14 +11,45 @@ What is the total of all the name scores in the file? ##--- """ +import string + def names_scores(filename): """ Returns the total of all the name scores in filename """ data = load_data(filename) + data.sort() # alphabetical + alpb = dict_alphabet() + score = 0 + inc = 1 + for name in data: + score += single_score(alpb, name, inc) + inc += 1 - - return 1 + return score + +def single_score(alpb, name, pos): + """ + Returns a name score given its position, the name and the alphabet + """ + score = 0 + for j in range(len(name)): + score += alpb[name[j]] + + return score * pos + +def dict_alphabet(): + """ + Returns a dict of the alphabet, with value for each letter. + """ + alpb= dict() + letters = string.ascii_uppercase + inc = 1 + for j in range(len(letters)): + alpb[letters[j]] = inc + inc += 1 + + return alpb def load_data(filename): """ @@ -31,5 +62,4 @@ def load_data(filename): return data if __name__ == '__main__': - data = load_data("e_22.data") - #print "Answer : %d" % (names_scores("e_22.data")) + print "Answer : %d" % (names_scores("e_22.data"))