aboutsummaryrefslogtreecommitdiff
path: root/challenge-002/paulo-custodio/lua/ch-2.lua
blob: 15a926434c5efcf6f04dcaa033750c16153cebf2 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env lua

--[[
Challenge 002

Challenge #2
Write a script that can convert integers to and from a base35
representation, using the characters 0-9 and A-Y. Dave Jacoby came up
with nice description about base35, in case you needed some background.
--]]

-- format a digit
function format_digit(n)
    if n<10 then
        return string.char(string.byte("0")+n)
    else
        return string.char(string.byte("A")+n-10)
    end
end

-- format a number
function format_number(n, base)
    local negative, output = false, ""
    if n<0 then negative, n = true, math.abs(n); end
    repeat
        local d = n % base
        n = math.floor(n / base)
        output = format_digit(d)..output
    until n==0
    if negative then output = "-"..output; end
    return output
end

-- main
if arg[1] == "-r" then
    opt_r, n = true, arg[2]
else
    opt_r, n = false, arg[1]
end
assert(n, "Usage: ch-2.lua [-r] nn")

if opt_r then
    io.write(tonumber(n, 35), "\n")
else
    io.write(format_number(tonumber(n), 35), "\n")
end