-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a8715a
commit 4d9b382
Showing
6 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Lua Implementation of Problem 22 | ||
================================ | ||
|
||
View source code :source:`lua/src/p0022.lua` | ||
|
||
Includes | ||
-------- | ||
|
||
- `utils.lua <./lib/utils.html>`__ | ||
|
||
Solution | ||
-------- | ||
|
||
.. lua:function:: solution() | ||
:return: The solution to problem 22 | ||
:rtype: number | ||
|
||
.. literalinclude:: ../../../lua/src/p0022.lua | ||
:language: Lua | ||
:linenos: | ||
|
||
.. tags:: word-problem, sorting, file-io |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-- Project Euler Problem 22 | ||
-- | ||
-- Problem: | ||
-- | ||
-- 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? | ||
|
||
local get_data_file = loadlib("utils").get_data_file | ||
|
||
return { | ||
solution = function() | ||
local answer = 0 | ||
local array = {get_data_file("p0022_names.txt"):gsub('"', ''):gmatch("([^,]+)")} | ||
table.sort(array) | ||
for idx, name in ipairs(array) do | ||
local score = 0 | ||
for i = 1,#name do | ||
score = score + string.byte(name, i) - string.byte('A', 1) + 1 | ||
end | ||
answer = answer + score * idx | ||
end | ||
return answer | ||
end | ||
} |
This file contains 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