aboutsummaryrefslogtreecommitdiff
path: root/challenge-002/paulo-custodio/python/ch-2.py
blob: 294987681637736c60c5dea0c6197ff6bacf1f02 (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
#!/usr/bin/env python

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

import sys

def format_digit(n):
    if n<10:
        return chr(n+ord('0'))
    else:
        return chr(n+ord('A')-10)

def format_number(n, base):
    negative = True if n<0 else False
    n = abs(n)
    output = ""
    while True:
        d = n % base
        n = int(n / base)
        output = format_digit(d) + output
        if n == 0: break
    if negative:
        output = "-" + output
    return output

def scan_digit(str):
    str = str.upper()
    if str >= "0" and str <= "9":
        return ord(str)-ord("0")
    elif str >= "A" and str <= "Z":
        return ord(str)-ord("A")+10
    else:
        return -1

def scan_number(str, base):
    n, negative = 0, False
    if str[0] == "-":
        str = str[1:]
        negative = True
    while str != "":
        d = scan_digit(str[0])
        str = str[1:]
        assert d>=0 and d<base
        n = n*base + d
    if negative:
        n = -n
    return n

if sys.argv[1] == "-r":
    print(scan_number(sys.argv[2], 35))
else:
    print(format_number(int(sys.argv[1]), 35))