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?

 

5천 개 이상의 이름이 들어 있는 46K 크기의 names.txt를(우클릭하고 "(으)로 링크 저장") 이용하여, 우선 알파벳 순서로 정렬하시오. 그리고, 목록의 알파벳 순서 위치와 알파벳 값을 곱하여 점수를 구하라.

예를 들어, 알파벳 순서로 정렬된 목록에서 COLIN의 값은 3+15+12+9+14=53이며, 목록의 938번째 있다. 따라서, COLIN의 점수는 938x53=49714이다.

파일에서 모든 이름 점수의 합계는 무엇인가?

--------------------------------------------------------------------------

 

파일을 이용하는 첫번째 문제이다. 파일로 읽어들인 문자열을 쉼표 기준으로 구분하여 따옴표(")를 제거하면서 리스트로 바꾸고, 알파벳 값으로 딕셔너리를 만들면 문제를 해결하기 위한 기초 작업은 끝난 것이다.

 

내장 함수를 이용해 정렬하고, 딕셔너리를 활용해 각 단어의 글자를 값으로 바꾸고 순서를 활용해 점수를 구하는 과정을 모든 단어를 대상으로 반복하면 된다.

+ Recent posts