저는 프로젝트 오일러의 프로그래밍 문제를 해결하고 있습니다. 도전 과제는 다음과 같습니다 :프로젝트 오일러 # 22 - 잘못된 논리?
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?
저는 커피 스크립트로 작성 했으므로 그 논리를 이해할 수 있도록 설명하겠습니다.
fs = require 'fs'
total = 0
fs.readFile './names.txt', (err,names) ->
names = names.toString().split(',')
names = names.sort()
for num in [0..(names.length-1)]
asc = 0
for i in [1..names[num].length]
asc += names[num].charCodeAt(i-1) - 64
total += num * asc
console.log total
그래서 기본적으로 파일을 읽고 있습니다. 이름을 배열로 분리하고 정렬합니다. 각 이름을 반복합니다. 루프를 반복하면서 각 문자를 통해 charCode (모든 대문자)를 가져옵니다. 그 다음 64로 뺀 다음 알파벳으로 위치를 잡습니다. 마지막으로, 전체 변수에 num of the loop * sum of positions of all letters
을 추가합니다.
내가 얻는 대답은 870873746
이지만 올바르지 않으며 다른 답변의 숫자는 약간 높습니다.
누구나 볼 수 있습니까?
것은 정확히이었다. 너무 많은 의미가있어, 나는 그것을 놓쳤다 고 믿을 수 없다. 감사! – Menztrual