Skip to content

Instantly share code, notes, and snippets.

@laughinghan
Created June 11, 2012 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laughinghan/2910974 to your computer and use it in GitHub Desktop.
Save laughinghan/2910974 to your computer and use it in GitHub Desktop.
Sexy, sexy Project Euler Problem 22 "Name Scores" Solution
import string
def name_scores():
inputfile = open('./Downloads/names.txt')
inputstring = inputfile.read()
stringlist = inputstring.split(',')
strlst = []
for smallstring in stringlist:
strlst.append(smallstring[1:-1])
strlst.sort()
counter = 1 # where to start?
translate = dict(zip(list(string.uppercase), range(1, 27)))
total = 0
for word in strlst:
value = 0
charlist = list(word)
for a in charlist:
value += translate[a]
total += value * counter
counter += 1
return total
'''
# this would have been cool, but "RuntimeError: maximum recursion depth exceeded
" How do you avoid this Han?
def mergesort(list):
if len(list) == 1:
return list
else:
list1 = list[:len(list) / 2]
list2 = list[len(list) / 2 + 1:]
out1 = mergesort(list1)
out2 = mergesort(list2)
out = merge(out1, out2)
return out
def merge(list1, list2):
oindex == 0
iindex1 == 0
iindex2 == 0
breaker == 0
while breaker != 1:
if list1[iindex1] <= list2[iindex2]:
outlist[oindex] = list1[iindex1]
oindex += 1
iindex1 += 1
else:
outlist[oindex] = list2[iindex2]
oindex += 1
iindex += 1
if iindex1 > (len(list1) - 1):
outlist.entend(list2[index2:])
breaker = 1
if iindex2 > (len(list2) - 1):
outlist.entend(list1[index1:])
breaker = 1
return outlist
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment