diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-09-02 11:13:08 +0430 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-09-02 11:13:08 +0430 |
| commit | 29b273fc146113e127bd1664dd8d198d4ad2dcbd (patch) | |
| tree | 53f3b22f0d6b2cf4efd2b55b9104874d3485b2aa /challenge-180/deadmarshal/lua/ch-1.lua | |
| parent | d1d8886281c19a0f6119fea49e2f3a9a1b068a89 (diff) | |
| download | perlweeklychallenge-club-29b273fc146113e127bd1664dd8d198d4ad2dcbd.tar.gz perlweeklychallenge-club-29b273fc146113e127bd1664dd8d198d4ad2dcbd.tar.bz2 perlweeklychallenge-club-29b273fc146113e127bd1664dd8d198d4ad2dcbd.zip | |
challenge180
Diffstat (limited to 'challenge-180/deadmarshal/lua/ch-1.lua')
| -rw-r--r-- | challenge-180/deadmarshal/lua/ch-1.lua | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/challenge-180/deadmarshal/lua/ch-1.lua b/challenge-180/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..0ce7829ce2 --- /dev/null +++ b/challenge-180/deadmarshal/lua/ch-1.lua @@ -0,0 +1,22 @@ +if #arg ~= 1 then + io.stderr:write('No arg(s) provided!') + os.exit(1) +end + +local function first_unique_character(str) + assert(type(str) == 'string', 'str must be a string!') + local chars, hash = {}, {} + str:gsub(".", function(c) table.insert(chars, c) end) + setmetatable(hash, {__index = function(t, k) return 0 end}) + for i, v in ipairs(chars) do + hash[v] = hash[v] + 1 + end + for i=1, #chars do + if hash[chars[i]] == 1 then + return string.format("%d as '%s' is the first unique character", + i-1, chars[i]) + end + end +end + +print(first_unique_character(arg[1])) |
