Solves Problem 22.

Works either on problem 17 or 18.
This commit is contained in:
Julien Lengrand-Lambert
2012-01-17 11:00:12 +01:00
parent a99806b09e
commit 8dac6fea28
3 changed files with 61 additions and 5 deletions

26
e_17.py Executable file
View File

@@ -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())

View File

@@ -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

38
e_22.py
View File

@@ -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"))