aboutsummaryrefslogtreecommitdiff
path: root/challenge-002/paulo-custodio/awk/ch-2.awk
blob: 752f93173b855e9a241c73fb8c2bc1c15fce7258 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/gawk

# 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.

function format_number(n, base,    negative, output, d) {
    if (n < 0) {
        negative = 1;
        n = -n
    }
    else {
        negative = 0;
    }

    output = ""
    do {
        d = n % base;
        n = int(n / base);
        output = substr(digits, d+1, 1) output;
    } while (n > 0)

    if (negative)
        output = "-" output;

    return output;
}

function scan_number(str, base,    n, negative, ch, d) {
    n = 0;
    if (sub(/^-/, "", str))
        negative = 1;
    else
        negative = 0;

    while (str != "") {
        ch = toupper(substr(str, 1, 1));
        str = substr(str, 2);
        d = index(digits, ch) - 1;
        if (d < 0 || d >= base) {
            print "cannot parse '" str "'";
            exit(1);
        }
        n = base * n + d;
    }
    if (negative)
        n = -n;
    return n;
}

BEGIN {
    BASE = 35;
    digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    if (ARGV[1] == "-r")
        print scan_number(ARGV[2], 35);
    else
        print format_number(ARGV[1], 35);
    exit 0
}