__author__ = 'Kalyan'

notes = '''
Write your own implementation of converting a number to a given base. It is important to have a good logical
and code understanding of this.

Till now, we were glossing over error checking, for this function do proper error checking and raise exceptions
as appropriate.

Reading material:
    http://courses.cs.vt.edu/~cs1104/number_conversion/convexp.html
'''
import string

def convert(number, base):
    """
    Convert the given number into a string in the given base. valid base is 2 <= base <= 36
    raise exceptions similar to how int("XX", YY) does (play in the console to find what errors it raises).
    Handle negative numbers just like bin and oct do.
    """
    if 'str' == type(base).__name__ or 'str' == type(number).__name__:
        raise TypeError
    if base < 2 or base > 36:
        raise ValueError
    else:
        l = []
        upper = string.uppercase
        numbers = range(10, 37, 1)
        match = dict(zip(numbers,upper))
        while number != 0:
            n = number.__mod__(base)
            if n > 10:
                l.append(match[n])
            elif n < 10:
                l.append(chr(n + 48))
            number = number.__div__(base)
        l.reverse()
        return "".join(l)


def test_to_base():
    assert "100" == convert(4,2)
    assert "FF" == convert(255,16)
    assert "377" == convert(255, 8)
    assert "JJ" == convert(399, 20)

    try:
        convert(10,1)
        assert False, "Invalid base <2 did not raise error"
    except ValueError as ve:
        print ve

    try:
        convert(10, 40)
        assert False, "Invalid base >40 did not raise error"
    except ValueError as ve:
        print ve

    try:
        convert("100", 10)
        assert False, "Invalid number did not raise error"
    except TypeError as te:
        print te

    try:
        convert(100, "10")
        assert False, "Invalid base did not raise error"
    except TypeError as te:
        print te
__author__ = 'Kalyan'

notes = '''
Write your own implementation of converting a number to a given base. It is important to have a good logical
and code understanding of this.

Till now, we were glossing over error checking, for this function do proper error checking and raise exceptions
as appropriate.

Reading material:
    http://courses.cs.vt.edu/~cs1104/number_conversion/convexp.html
'''
import string

def convert(number, base):
    """
    Convert the given number into a string in the given base. valid base is 2 <= base <= 36
    raise exceptions similar to how int("XX", YY) does (play in the console to find what errors it raises).
    Handle negative numbers just like bin and oct do.
    """
    if 'str' == type(base).__name__ or 'str' == type(number).__name__:
        raise TypeError
    if base < 2 or base > 36:
        raise ValueError
    else:
        l = []
        upper = string.uppercase
        numbers = range(10, 37, 1)
        match = dict(zip(numbers,upper))
        while number != 0:
            n = number.__mod__(base)
            if n > 10:
                l.append(match[n])
            elif n < 10:
                l.append(chr(n + 48))
            number = number.__div__(base)
        l.reverse()
        return "".join(l)


def test_to_base():
    assert "100" == convert(4,2)
    assert "FF" == convert(255,16)
    assert "377" == convert(255, 8)
    assert "JJ" == convert(399, 20)

    try:
        convert(10,1)
        assert False, "Invalid base <2 did not raise error"
    except ValueError as ve:
        print ve

    try:
        convert(10, 40)
        assert False, "Invalid base >40 did not raise error"
    except ValueError as ve:
        print ve

    try:
        convert("100", 10)
        assert False, "Invalid number did not raise error"
    except TypeError as te:
        print te

    try:
        convert(100, "10")
        assert False, "Invalid base did not raise error"
    except TypeError as te:
        print te
Write your own implementation of converting a number to a given base. It is important to have a good logical and code understanding of this.  Till now, we were glossing over error checking, for this function do proper error checking and raise exceptions as appropriate.  Reading material:     http://courses.cs.vt.edu/~cs1104/number_conversion/convexp.html Convert the given number into a string in the given base. valid base is 2 <= base <= 36     raise exceptions similar to how int("XX", YY) does (play in the console to find what errors it raises).     Handle negative numbers just like bin and oct do.