Names scores
Using names.txt
(right click and 'Save Link/Target As...'), a 46K text file containing
over five-thousand first names, begin by sorting it into alphabetical
order. Then working out the alphabetical value for each name, multiply
this value by its alphabetical position in the list to obtain a name
score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?
To solve this problem we will have to use the file objects which will make our work simpler. Also there is a need for using a few slicing operations. There is no big algorithm that will make you confusing so just have a look at the following links and you will be good to go😃.
Program
Program is very simple and is as follows:
Remember
ord(character)
will give the ASCII value of the character. As all the characters in the text file are in capitals I have subtracted 64 as the value of 'A' is 65 and we wanted 'A' to be 1, 'B' to be 2 and so on.
consider a string
x = 'Hello'
and x[1:-1]
will give me 'ell'
.
If you want to download above code then you can download it from Github Gist pep22.py
Output
Summary
This program is again very simple and there is no need for searching for anything if you have already solved the previous problems.
As always if you have any doubt or didn't understand anything, or have a better solution, or have a different solution, or have any suggestions, then please don't hesitate to comment. I will be glad to comment back.
You can also contact me.
Thank you. Have a nice day😄.