For names sake - Project Euler 22
up vote
2
down vote
favorite
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())
programming-challenge file julia
add a comment |
up vote
2
down vote
favorite
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())
programming-challenge file julia
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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())
programming-challenge file julia
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())
programming-challenge file julia
programming-challenge file julia
edited Oct 16 '17 at 4:05
Phrancis
14.7k646139
14.7k646139
asked Oct 16 '17 at 3:43
N3buchadnezzar
2,5251332
2,5251332
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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.
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
Pretty sure you wantInt(char) - Int('A') + 1, and a similar change for your second version, since'A'should evaluate to1, not0.
– AJNeufeld
Oct 23 at 18:18
add a comment |
up vote
0
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
Pretty sure you wantInt(char) - Int('A') + 1, and a similar change for your second version, since'A'should evaluate to1, not0.
– AJNeufeld
Oct 23 at 18:18
add a comment |
up vote
0
down vote
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.
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
Pretty sure you wantInt(char) - Int('A') + 1, and a similar change for your second version, since'A'should evaluate to1, not0.
– AJNeufeld
Oct 23 at 18:18
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Oct 16 '17 at 4:29
Oscar Smith
2,698922
2,698922
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
Pretty sure you wantInt(char) - Int('A') + 1, and a similar change for your second version, since'A'should evaluate to1, not0.
– AJNeufeld
Oct 23 at 18:18
add a comment |
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
Pretty sure you wantInt(char) - Int('A') + 1, and a similar change for your second version, since'A'should evaluate to1, not0.
– AJNeufeld
Oct 23 at 18:18
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
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?
– N3buchadnezzar
Oct 16 '17 at 12:40
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.– AJNeufeld
Oct 23 at 18:18
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.– AJNeufeld
Oct 23 at 18:18
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Jun 25 at 13:53
sundar
1485
1485
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f178003%2ffor-names-sake-project-euler-22%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown