#!python

import string
# 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)
    # TODO: Decode digits from binary (base 2)
    # ...
    # TODO: Decode digits from hexadecimal (base 16)
    # ...
    # TODO: Decode digits from any base (2 up to 36)
    # ...

    result = 0

    for index, digit in enumerate(digits):
        if digit.isdigit():
            digit_to_add = int(digit)
        else:
            #  ord() covert string to single digit unicode number
            digit_to_add = ord(digit) - 97 + 10
        result += digit_to_add
        if index is not len(digits) - 1:
            result *= base
        else:
            1
    return result


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)
    # TODO: Encode number in binary (base 2)
    # ...
    # TODO: Encode number in hexadecimal (base 16)
    # ...
    # TODO: Encode number in any base (2 up to 36)
    # ...
    new_base_number = ''
    while number != 0:
        remainder = number % base
        number = number // base
        if(remainder >= 10 and base > 10):
            letters = "abcdefghijklmnopqrstuvwxyz"
            remainder = letters[remainder - 10]
        else:
            remainder
        new_base_number += str(remainder)
    new_base_number = new_base_number[::-1]
    return new_base_number


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)
    # TODO: Convert digits from base 2 to base 16 (and vice versa)
    # ...
    # TODO: Convert digits from base 2 to base 10 (and vice versa)
    # ...
    # TODO: Convert digits from base 10 to base 16 (and vice versa)
    # ...
    # TODO: Convert digits from any base to any base (2 up to 36)
    # ...
    if(base1 == 10):
        new_base_number = encode(int(digits), base2)
    else:
        decimal_digit = decode(digits, base1)
        new_base_number = encode(decimal_digit, base2)
    return new_base_number


# if base1 == 10:
#         new_base_number = encode(int(decimal_digit), base2)
#     else:
#         decimal_digit = decode(digits, base1)
#         new_base_number = encode(decimal_digit, base2)
#     # Return
#     return new_base_number


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()
    result = encode(1, 2)
    print("RESULT", result)
#!python

import string
# 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)
    # TODO: Decode digits from binary (base 2)
    # ...
    # TODO: Decode digits from hexadecimal (base 16)
    # ...
    # TODO: Decode digits from any base (2 up to 36)
    # ...

    result = 0

    for index, digit in enumerate(digits):
        if digit.isdigit():
            digit_to_add = int(digit)
        else:
            #  ord() covert string to single digit unicode number
            digit_to_add = ord(digit) - 97 + 10
        result += digit_to_add
        if index is not len(digits) - 1:
            result *= base
        else:
            1
    return result


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)
    # TODO: Encode number in binary (base 2)
    # ...
    # TODO: Encode number in hexadecimal (base 16)
    # ...
    # TODO: Encode number in any base (2 up to 36)
    # ...
    new_base_number = ''
    while number != 0:
        remainder = number % base
        number = number // base
        if(remainder >= 10 and base > 10):
            letters = "abcdefghijklmnopqrstuvwxyz"
            remainder = letters[remainder - 10]
        else:
            remainder
        new_base_number += str(remainder)
    new_base_number = new_base_number[::-1]
    return new_base_number


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)
    # TODO: Convert digits from base 2 to base 16 (and vice versa)
    # ...
    # TODO: Convert digits from base 2 to base 10 (and vice versa)
    # ...
    # TODO: Convert digits from base 10 to base 16 (and vice versa)
    # ...
    # TODO: Convert digits from any base to any base (2 up to 36)
    # ...
    if(base1 == 10):
        new_base_number = encode(int(digits), base2)
    else:
        decimal_digit = decode(digits, base1)
        new_base_number = encode(decimal_digit, base2)
    return new_base_number


# if base1 == 10:
#         new_base_number = encode(int(decimal_digit), base2)
#     else:
#         decimal_digit = decode(digits, base1)
#         new_base_number = encode(decimal_digit, base2)
#     # Return
#     return new_base_number


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()
    result = encode(1, 2)
    print("RESULT", result)
#!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] # TODO: Decode digits from binary (base 2) # ... # TODO: Decode digits from hexadecimal (base 16) # ... # TODO: Decode digits from any base (2 up to 36) # ... #  ord() covert string to single digit unicode number 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 # TODO: Encode number in binary (base 2) # ... # TODO: Encode number in hexadecimal (base 16) # ... # TODO: Encode number in any base (2 up to 36) # ... 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] # TODO: Convert digits from base 2 to base 16 (and vice versa) # ... # TODO: Convert digits from base 2 to base 10 (and vice versa) # ... # TODO: Convert digits from base 10 to base 16 (and vice versa) # ... # TODO: Convert digits from any base to any base (2 up to 36) # ... # if base1 == 10: #         new_base_number = encode(int(decimal_digit), base2) #     else: #         decimal_digit = decode(digits, base1) #         new_base_number = encode(decimal_digit, base2) #     # Return #     return new_base_number Read command-line arguments and convert given digits between bases. # Ignore script file name # Convert given digits between bases # main()