Exemple #1
0
def base_converter(dec_number, base):
    """
       A decimal to binary convertion function using 
       the divide_by_2 Algorithm 
       this function converts from any base to binary 
       -Implemented usign the stack data structure 
    """

    digits = "0123456789ABCDEF"

    rem_stack = Bstack()

    while dec_number > 0:
        rem = dec_number % base
        rem_stack.push(rem)

        dec_number = dec_number // base

    new_string = ""

    while not rem_stack.is_empty():

        new_string = new_string + digits[rem_stack.pop()]

    return new_string
Exemple #2
0
def par_checker(userstring):
	"""
		This function is only applicable to structuring 
		parenthesis ( and ) 
		31/12/2015  after my laptop spoilt 
		so am using a desktop to do the coding 

	"""
	correct = True
	stack = Bstack()
	error = ""

	for xter in userstring :
		if xter == "(":
			#put into the stack 
			stack.push(xter)
			print(stack.get_items())
		elif xter == ")":
			try:
				stack.pop() # remove one ( from stack 
				
			except IndexError, e:
				correct = False
		else:
			correct = False
			error = "Improper character in userstring !"
Exemple #3
0
def divide_by_2(dec_number):

    rem_stack = Bstack()

    while dec_number > 0:
        rem = dec_number % 2
        rem_stack.push(rem)

        dec_number = dec_number // 2

    bin_string = ""
    while not rem_stack.is_empty():
        bin_string = bin_string + str(rem_stack.pop())

    return bin_string