2
\$\begingroup\$

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())
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 Oct 16 '17 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 to 1, not 0. \$\endgroup\$ – AJNeufeld Oct 23 '18 at 18:18
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.