diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-09-02 11:08:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-02 11:08:44 +0100 |
| commit | 8162d93f3469587093f2c5a0d6e8cbdef9434e47 (patch) | |
| tree | be5db0f4e83e316b47fbcc702ebfa6979e8d395d /challenge-180/deadmarshal/lua/ch-1.lua | |
| parent | 91d665102ab1b10f30b41bcf87195be8565053ae (diff) | |
| parent | 29b273fc146113e127bd1664dd8d198d4ad2dcbd (diff) | |
| download | perlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.tar.gz perlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.tar.bz2 perlweeklychallenge-club-8162d93f3469587093f2c5a0d6e8cbdef9434e47.zip | |
Merge pull request #6683 from deadmarshal/challenge180
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])) |
