Rebased projecteuler folder, now includes all contest programming stuff
This commit is contained in:
40
projecteuler/022/main.py
Normal file
40
projecteuler/022/main.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
# Calculate the sum of all scores of the names in the textfile, where
|
||||
# the score is the product of the sorted position and
|
||||
# sum of alphabetic values of its letters
|
||||
|
||||
def charScore(c):
|
||||
assert len(c) == 1
|
||||
|
||||
return ord(c.lower()) - 96
|
||||
|
||||
def strScore(s):
|
||||
res = 0
|
||||
for c in s:
|
||||
res = res + charScore(c)
|
||||
|
||||
return res
|
||||
|
||||
def main():
|
||||
print("Hello, this is Patrick")
|
||||
|
||||
f = open("names.txt", 'r')
|
||||
names = f.readline()
|
||||
f.close()
|
||||
|
||||
names = names.strip('\"').split("\",\"")
|
||||
names.sort()
|
||||
|
||||
scores = []
|
||||
for i in range(len(names)):
|
||||
n = names[i]
|
||||
score = strScore(n) * (i + 1)
|
||||
scores.append(score)
|
||||
|
||||
print(sum(scores))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
1
projecteuler/022/names.txt
Normal file
1
projecteuler/022/names.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user