aboutsummaryrefslogtreecommitdiff
path: root/challenge-091/paulo-custodio/lua/ch-1.lua
blob: 3091ff1ae36f19bee941cab542657db4c209d24a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env lua

--[[
Challenge 091

TASK #1: Count Number

You are given a positive number $N. Write a script to count number and
display as you read it.

Solution with regular expressions:
Just match the first digit and a sequence of equal matches, capture the
results and show them.
--]]

function count_number(n)
    local inp, out = tostring(n), ''
    while (inp ~= '') do
        local digit = string.match(inp, "^%d")
        local digits = string.match(inp, "^"..digit.."+")
        out = out..tostring(#digits)..digit
        inp = string.sub(inp, #digits+1)
    end
    return out
end

print(count_number(tonumber(arg[1])))