Rebased projecteuler folder, now includes all contest programming stuff

This commit is contained in:
2021-10-26 10:54:24 +02:00
parent 1aa6120838
commit e0c627a384
77 changed files with 203 additions and 67 deletions

40
projecteuler/022/main.py Normal file
View 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()