Project Euler - 22
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?
While being fairly comfortable with the zen of Python, Julia is very new to me. Is the following code in line with Julia's vision?
In particular I feel my handling of the dictionary is sub-optimal and especially the way i hard-code remove the parenthesis in names.
Every hint on how to improve the code is welcome.
ALPHABET_INDEX = Dict( letter => index for (index, letter) in enumerate('A':'Z'))
function namescore(name)
return sum(ALPHABET_INDEX[letter] for letter in name[2:end-1])
end
function sort_file(filename)
file = open(filename)
sort(split(readstring(filename),","))
end
function PE_022(filename="p022_names.txt")
total = 0
for (index, name) in enumerate(sort_file(filename))
total += index*namescore(name)
end
total
end
println(PE_022())
2 Answers 2
the only change I would make is to remove your use of the Dict
. Dicts are great when you need mutability, but here there is the much simpler solution Int(letter)-Int('A')
. With this change, namescore
becomes
function namescore(name)
return sum(Int(char)-Int('A') for letter in name[2:end-1])
end
This, however is not ideal, as we can take out the subtraction, yielding
function namescore(name)
return sum(Int(char) for letter in name[2:end-1]) - (lenth(name)-2) * Int('A')
end
I haven't done performance testing, but this should be a fair bit faster, as I would expect Int
to be faster than a dictionary lookup.
-
\$\begingroup\$ I just used 'BenchmarkTools' , and saw no noticable change in performance. Here are the results, and here are the functions tested. However I do agree that using 'Int(letter)' is much cleaner. Perhaps dictionaries in Julia are fairly optimized compared to Python? \$\endgroup\$N3buchadnezzar– N3buchadnezzar2017年10月16日 12:40:05 +00:00Commented Oct 16, 2017 at 12:40
-
\$\begingroup\$ Pretty sure you want
Int(char) - Int('A') + 1
, and a similar change for your second version, since'A'
should evaluate to1
, not0
. \$\endgroup\$AJNeufeld– AJNeufeld2018年10月23日 18:18:31 +00:00Commented Oct 23, 2018 at 18:18
I solved the same problem a few months ago, here's the code I used:
name_scores_total() = name_scores_total(@__DIR__()*"/022data_names.txt")
name_scores_total(names_file::String) = name_scores_total(vec(readdlm(names_file, ',', String)))
function name_scores_total(names_list::Array{String})
sort!(names_list)
alphabet_order = Dict((c, UInt8(c) - UInt8('A') + 1) for c in 'A':'Z')
total_score = 0
for (pos, name) in enumerate(names_list)
try
name_score = pos * sum(alphabet_order[c] for c in name)
total_score += name_score
catch er
er isa KeyError && error("Only CAPITAL English letters are allowed in the file ($(name) has $(er.key) in it)")
rethrow()
end
end
total_score
end
if !isinteractive()
println(name_scores_total())
end
Pretty similar to what your code does when it comes down to it (the main loop), but uses multiple dispatch to allow different types of input, and uses readdlm
to read the file, which automatically strips the quotes around the input fields.
I can't say for sure that this is a "better" way of doing things since I'm also still getting a feel for the language, but at the least it just offers a different approach.