여기 5천개 이상의 영문 이름들이 들어있는 46KB짜리 텍스트 파일 names.txt 이 있습니다 (우클릭해서 다운로드 받으세요).
이제 각 이름에 대해서 아래와 같은 방법으로 점수를 매기고자 합니다.
- 먼저 모든 이름을 알파벳 순으로 정렬합니다.
- 각 이름에 대해서, 그 이름을 이루는 알파벳에 해당하는 숫자(A=1, B=2, ..., Z=26)를 모두 더합니다.
- 여기에 이 이름의 순번을 곱합니다.
예를 들어 "COLIN"의 경우, 알파벳에 해당하는 숫자는 3, 15, 12, 9, 14이므로 합이 53, 그리고 정렬했을 때 938번째에 오므로 최종 점수는 938 × 53 = 49714가 됩니다.
names.txt에 들어있는 모든 이름의 점수를 계산해서 더하면 얼마입니까?
로직이 어렵지는 않았으나
알고있는 API들이 한정되어있는 새로운 언어로 생각을 구현하는 부분이
어려웠고... 디버깅이 참 힘들었음.
This file contains hidden or 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
#generate alphabet score | |
#alphaList=map(chr, range(ord('A'), ord('Z'))) | |
alphaList = [chr(c) for c in range(ord('A'), ord('Z')+1)] | |
print (alphaList) | |
def getNameAndIndex(dataList): | |
for index in range(0, len(dataList)): | |
name = dataList[index] | |
print("name={0}, index={1}".format(name, index+1)) | |
yield name, index+1 | |
def getNameScore(name): | |
sum=0 | |
for c in name: | |
sum=sum+alphaList.index(c)+1 | |
print("c={0}, score={1}, namescore={2}".format(c, alphaList.index(c)+1, sum)) | |
return sum | |
totalScore=0 | |
indexOfName=0 | |
sampleFile=open("names.txt", "r") | |
names=sampleFile.readlines() | |
namesStr=names[0] | |
namesStr=namesStr.rstrip("\n") | |
#print (namesStr.replace("\"","").split(",")) | |
nameList = namesStr.replace("\"","").split(",") | |
nameList.sort() | |
for name, index in getNameAndIndex(nameList): | |
(oldTotalScore, totalScore) = (totalScore, totalScore + (getNameScore(name) * (index))) | |
print ("total score : {0}, minus : {1}".format(totalScore, totalScore-oldTotalScore)) | |
print ("total score : {0}".format(totalScore)) |
xinuguru님의 코드
This file contains hidden or 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
wordlist = [word.strip('"') for word in open('names.txt').read().split(',')] | |
wordlist.sort() | |
result = 0 | |
for i, word in enumerate(wordlist): | |
result += sum([ord(c)-ord('A')+1 for c in word]) * (i+1) | |
print result |
Ruby
This file contains hidden or 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
def getNameAndIndex(dataList) | |
for index in (0..dataList.length-1) | |
name = dataList[index] | |
puts "name=#{name}, index=#{index}" | |
yield name, index+1 | |
end | |
end | |
alphaList=Array.new | |
('A'..'Z').each {|c| alphaList.push(c)} | |
def getNameScore(name) | |
sum=0 | |
name.each_char do | |
|c| sum = 1 | |
puts "c=#{c}, score=#{alphaList.index(c)+1}, namescore=#{sum}" | |
end | |
return sum | |
end | |
namesStr="" | |
File.open("names.txt") do |file| | |
while line = file.gets | |
namesStr=line | |
end | |
end | |
namesStr = namesStr.gsub(/\"/, "") | |
namesList = namesStr.split(",") | |
namesList.sort! | |
puts namesList | |
totalScore = 0 | |
getNameAndIndex(namesList) do | |
|name, index| | |
nameScore = 0 | |
name.rstrip! | |
name.each_char do | |
|c| | |
puts "c : #{c}, name : #{name}" | |
nameScore = nameScore + alphaList.index(c) + 1 | |
puts "total score : #{totalScore}, al : #{alphaList.index(c)}, c : #{c}" | |
end | |
totalScore = totalScore + (nameScore * index) | |
puts "total score : #{totalScore}, name score : #{nameScore}, name index : #{index}" | |
end | |
puts "total score : #{totalScore}" |
Perl
This file contains hidden or 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
use strict; | |
use warnings; | |
use List::MoreUtils qw(first_index); | |
open PRB, "names.txt"; | |
my $names_str; | |
while(defined(my $line=)) { | |
chomp($line); | |
$names_str = $line; | |
} | |
$names_str =~ s/\"//g; | |
my @names_list=split /,/, $names_str; | |
@names_list = sort @names_list; | |
my @alpha_list=('A'..'Z'); | |
my $index=0; | |
my $total_score=0; | |
foreach my $name (@names_list) { | |
$index = first_index {$_ eq $name} @names_list; | |
$index = $index + 1; #곱하기를 위함 | |
#이름 자체의 점수를 계산 | |
my $char_score_sum = 0; | |
foreach my $byte (split //, $name) { | |
my $char_score = first_index {$_ eq $byte} @alpha_list; | |
print "byte : $byte , char_score : $char_score \n"; | |
$char_score_sum = $char_score_sum + $char_score + 1; | |
} | |
print "name : $name, index : $index, $char_score_sum : $char_score_sum \n"; | |
#총 점수의 합 | |
$total_score = $total_score + ($char_score_sum * $index); | |
} | |
print "total_score : $total_score"; |