Created
February 10, 2020 17:06
-
-
Save AmosAidoo/e392af22cd73851b522c7fe953ef99cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Function to caluculate the value of a string | |
#Parameter: string, a string from the file | |
#Parameter: position, position of string in the file | |
def calculate_value(string, position): | |
worth = 0 | |
for c in string: | |
worth = worth + (ord(c) - 64) | |
return worth * position | |
#Variable to keep names | |
read_data = None | |
#Read data into list | |
with open('names.txt') as f: | |
read_data = f.read() | |
#Split data into array and remove double quotes("") surrounding the string | |
read_data = read_data.replace('"', "").split(',') | |
#Sort the list | |
read_data.sort() | |
#Store the length of the list | |
n = len(read_data) | |
#Store the answer | |
ans = 0 | |
#Loop through the strings and calculate their values | |
for i in range(1,n+1): | |
ans = ans + calculate_value(read_data[i-1], i) | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment