diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
| commit | 536dafb989fad8da4c2df0df1bfc22eb2fac3706 (patch) | |
| tree | c2b39c685cca7cc2ed3b4ab4e91e75f5b9a71fdd /challenge-002/abigail/python | |
| parent | e36daeee6e93355383ad3a1c3fc43271f9a357d7 (diff) | |
| parent | c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae (diff) | |
| download | perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.gz perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.bz2 perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-002/abigail/python')
| -rw-r--r-- | challenge-002/abigail/python/ch-1.py | 14 | ||||
| -rw-r--r-- | challenge-002/abigail/python/ch-2.py | 63 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-002/abigail/python/ch-1.py b/challenge-002/abigail/python/ch-1.py new file mode 100644 index 0000000000..de6839af0a --- /dev/null +++ b/challenge-002/abigail/python/ch-1.py @@ -0,0 +1,14 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + print int (line) diff --git a/challenge-002/abigail/python/ch-2.py b/challenge-002/abigail/python/ch-2.py new file mode 100644 index 0000000000..22fe4fcaf0 --- /dev/null +++ b/challenge-002/abigail/python/ch-2.py @@ -0,0 +1,63 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# 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, 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)) |
