aboutsummaryrefslogtreecommitdiff
path: root/challenge-002/abigail/python/ch-2.py
blob: b00e0dd541a70580435477e2b38ca7d3cd2c6067 (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
#!/opt/local/bin/python

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-002
#

#
# Run as: python ch-2.py {-f | -t} < input-file
#

import fileinput
import sys
import getopt

BASE = 35

#
# Parse options
#
do_to_base   = 0
do_from_base = 0
opts, args = getopt . getopt (sys . argv [1:], 'ft')

for opt, val in opts:
    if   opt == "-f":
        do_from_base = 1
    elif opt == "-t":
        do_to_base = 1

if do_to_base + do_from_base != 1:
    print ("Need exactly one of -f or -t")
    sys . exit (1)


#
# Translate a base 10 number to base 35
#
def to_base (number):
    out = ""
    while number > 0:
        rest = number % BASE
        if rest < 10:
            char = str (rest)
        else:
            char = chr (65 + rest - 10)
        out = char + out
        number = int (number / BASE)
    return out


#
# Translate a number from base BASE to base 10
#
def from_base (number):
    return int (number . strip (), BASE)
    
#
# Need to clean argv, else fileinput will try to open a file
#
sys . argv [1:] = []

for line in fileinput . input ():
    print (from_base (line) if do_from_base else to_base (int (line)))