40 lines
731 B
Python
40 lines
731 B
Python
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() |