diff options
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])) |
