Project Euler Problem#22
Varjinth subramaniyan
1 min read
'''pthon prgram to find alphabetical value for each name in a text file and
multiply this value by its alphabetical position'''
file = open('p022_names.txt', "r")
file_contents = file.readlines()
file.close()
for line in file_contents:
a=line[1:-1].split('","')
a.sort()
dictionary = {}
for i in range(26):
key = chr(ord('A') + i)
value = i + 1
dictionary[key] = value
sum=0
for i in a:
sum1=0
for j in i:
sum1+=dictionary[j]
sum+=(sum1*(a.index(i)+1))
print(sum) #31626
0
Subscribe to my newsletter
Read articles from Varjinth subramaniyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by