<reponame>lvreynoso/CS1.3-Coursework
#!python

import string
import math
# Hint: Use these string constants to encode/decode hexadecimal digits and more
# string.digits is '0123456789'
# string.hexdigits is '0123456789abcdefABCDEF'
# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'
# string.ascii_uppercase is 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# string.ascii_letters is ascii_lowercase + ascii_uppercase
# string.printable is digits + ascii_letters + punctuation + whitespace

def decode(digits, base):
    """Decode given digits in given base to number in base 10.
    digits: str -- string representation of number (in given base)
    base: int -- base of given number
    return: int -- integer representation of number (in base 10)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
    # reverse the string
    little_endian = digits[::-1]
    decimal = 0
    for power in range(len(little_endian)):
        ordinal = string.printable.index(little_endian[power])
        decimal += ordinal * (base ** power)
    return decimal


def encode(number, base):
    """Encode given number in base 10 to digits in given base.
    number: int -- integer representation of number (in base 10)
    base: int -- base to convert to
    return: str -- string representation of number (in given base)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
    # Handle unsigned numbers only for now
    assert number >= 0, 'number is negative: {}'.format(number)
    highest_power = int(math.floor(math.log(number, base)))
    value_left_to_convert = number
    output = ""
    for power in range(highest_power, -1, -1):
        if value_left_to_convert > 0:
            glyph_decimal_value = value_left_to_convert // (base ** power)
            value_left_to_convert -= glyph_decimal_value * (base ** power)
            output += string.printable[glyph_decimal_value]
        else:
            output += "0"
    return output


def convert(digits, base1, base2):
    """Convert given digits in base1 to digits in base2.
    digits: str -- string representation of number (in base1)
    base1: int -- base of given number
    base2: int -- base to convert to
    return: str -- string representation of number (in base2)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base1 <= 36, 'base1 is out of range: {}'.format(base1)
    assert 2 <= base2 <= 36, 'base2 is out of range: {}'.format(base2)
    decoded = decode(digits, base1)
    encoded = encode(decoded, base2)
    return encoded


def main():
    """Read command-line arguments and convert given digits between bases."""
    import sys
    args = sys.argv[1:]  # Ignore script file name
    if len(args) == 3:
        digits = args[0]
        base1 = int(args[1])
        base2 = int(args[2])
        # Convert given digits between bases
        result = convert(digits, base1, base2)
        print('{} in base {} is {} in base {}'.format(digits, base1, result, base2))
    else:
        print('Usage: {} digits base1 base2'.format(sys.argv[0]))
        print('Converts digits from base1 to base2')


if __name__ == '__main__':
    main()

#!python

import string
import math
# Hint: Use these string constants to encode/decode hexadecimal digits and more
# string.digits is '0123456789'
# string.hexdigits is '0123456789abcdefABCDEF'
# string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz'
# string.ascii_uppercase is 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# string.ascii_letters is ascii_lowercase + ascii_uppercase
# string.printable is digits + ascii_letters + punctuation + whitespace

def decode(digits, base):
    """Decode given digits in given base to number in base 10.
    digits: str -- string representation of number (in given base)
    base: int -- base of given number
    return: int -- integer representation of number (in base 10)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
    # reverse the string
    little_endian = digits[::-1]
    decimal = 0
    for power in range(len(little_endian)):
        ordinal = string.printable.index(little_endian[power])
        decimal += ordinal * (base ** power)
    return decimal


def encode(number, base):
    """Encode given number in base 10 to digits in given base.
    number: int -- integer representation of number (in base 10)
    base: int -- base to convert to
    return: str -- string representation of number (in given base)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base <= 36, 'base is out of range: {}'.format(base)
    # Handle unsigned numbers only for now
    assert number >= 0, 'number is negative: {}'.format(number)
    highest_power = int(math.floor(math.log(number, base)))
    value_left_to_convert = number
    output = ""
    for power in range(highest_power, -1, -1):
        if value_left_to_convert > 0:
            glyph_decimal_value = value_left_to_convert // (base ** power)
            value_left_to_convert -= glyph_decimal_value * (base ** power)
            output += string.printable[glyph_decimal_value]
        else:
            output += "0"
    return output


def convert(digits, base1, base2):
    """Convert given digits in base1 to digits in base2.
    digits: str -- string representation of number (in base1)
    base1: int -- base of given number
    base2: int -- base to convert to
    return: str -- string representation of number (in base2)"""
    # Handle up to base 36 [0-9a-z]
    assert 2 <= base1 <= 36, 'base1 is out of range: {}'.format(base1)
    assert 2 <= base2 <= 36, 'base2 is out of range: {}'.format(base2)
    decoded = decode(digits, base1)
    encoded = encode(decoded, base2)
    return encoded


def main():
    """Read command-line arguments and convert given digits between bases."""
    import sys
    args = sys.argv[1:]  # Ignore script file name
    if len(args) == 3:
        digits = args[0]
        base1 = int(args[1])
        base2 = int(args[2])
        # Convert given digits between bases
        result = convert(digits, base1, base2)
        print('{} in base {} is {} in base {}'.format(digits, base1, result, base2))
    else:
        print('Usage: {} digits base1 base2'.format(sys.argv[0]))
        print('Converts digits from base1 to base2')


if __name__ == '__main__':
    main()
#!python # Hint: Use these string constants to encode/decode hexadecimal digits and more # string.digits is '0123456789' # string.hexdigits is '0123456789abcdefABCDEF' # string.ascii_lowercase is 'abcdefghijklmnopqrstuvwxyz' # string.ascii_uppercase is 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # string.ascii_letters is ascii_lowercase + ascii_uppercase # string.printable is digits + ascii_letters + punctuation + whitespace Decode given digits in given base to number in base 10.     digits: str -- string representation of number (in given base)     base: int -- base of given number     return: int -- integer representation of number (in base 10) # Handle up to base 36 [0-9a-z] # reverse the string Encode given number in base 10 to digits in given base.     number: int -- integer representation of number (in base 10)     base: int -- base to convert to     return: str -- string representation of number (in given base) # Handle up to base 36 [0-9a-z] # Handle unsigned numbers only for now Convert given digits in base1 to digits in base2.     digits: str -- string representation of number (in base1)     base1: int -- base of given number     base2: int -- base to convert to     return: str -- string representation of number (in base2) # Handle up to base 36 [0-9a-z] Read command-line arguments and convert given digits between bases. # Ignore script file name # Convert given digits between bases