Contents

  1. ./binaryToDecimalIterateOverCharacters.py
  2. ./binaryToDecimalIterateOverExponents.py
  3. ./binaryToDecimal.py
  4. ./decimalToBinary.py

./binaryToDecimalIterateOverCharacters.py 1/4

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the characters in the string,
# i.e., from left to right in the string.
# By CSCI111

import test

def main():
    print("This program converts a binary number into a decimal number.")
    
    binaryNum = input("What is your binary number? ")
    decVal = binaryToDecimal(binaryNum)
    print("The decimal value of", binaryNum, "is", decVal)


def testBinaryToDecimal():
    test.testEqual( binaryToDecimal("0"), 0 )
    test.testEqual( binaryToDecimal("0000"), 0 )
    test.testEqual( binaryToDecimal("1"), 1 )
    test.testEqual( binaryToDecimal("0001"), 1 )
    test.testEqual( binaryToDecimal("10"), 2 )
    test.testEqual( binaryToDecimal("0010"), 2 )
    test.testEqual( binaryToDecimal("111"), 7 )
    test.testEqual( binaryToDecimal("1000"), 8 )
    test.testEqual( binaryToDecimal("1001"), 9 )
    test.testEqual( binaryToDecimal("1101"), 13 )
    test.testEqual( binaryToDecimal("1111"), 15 )
    test.testEqual( binaryToDecimal("10000"), 16 )
    test.testEqual( binaryToDecimal("10101"), 21 )
    test.testEqual( binaryToDecimal("10110"), 22 )
    test.testEqual( binaryToDecimal("11111"), 31 )
    test.testEqual( binaryToDecimal("100000"), 32 )
    test.testEqual( binaryToDecimal("101011"), 43 )
    test.testEqual( binaryToDecimal("110011"), 51 )
    
def binaryToDecimal(binary):
    """
    Converts binary number into a decimal number
    Pre: binaryNum should be a string containing only 0s and 1s
    Post: converts binaryNum into a decimal value and returns that value
    as an integer
    """
    decVal = 0
    exponent = len(binary)
    
    for char in binary:
        bit = int(char)
        exponent -= 1
        decVal += bit * 2**exponent
        
    return decVal
    
#testBinaryToDecimal()
main()

./binaryToDecimalIterateOverExponents.py 2/4

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents,
# i.e., from right to left over the binary number.
# By CSCI111

import test

def main():
    print("This program converts a binary number into a decimal number.")
    
    binaryNum = input("What is your binary number? ")
    decVal = binaryToDecimal(binaryNum)
    print("The decimal value of", binaryNum, "is", decVal)


def testBinaryToDecimal():
    test.testEqual( binaryToDecimal("0"), 0 )
    test.testEqual( binaryToDecimal("0000"), 0 )
    test.testEqual( binaryToDecimal("1"), 1 )
    test.testEqual( binaryToDecimal("0001"), 1 )
    test.testEqual( binaryToDecimal("10"), 2 )
    test.testEqual( binaryToDecimal("0010"), 2 )
    test.testEqual( binaryToDecimal("111"), 7 )
    test.testEqual( binaryToDecimal("1000"), 8 )
    test.testEqual( binaryToDecimal("1001"), 9 )
    test.testEqual( binaryToDecimal("1101"), 13 )
    test.testEqual( binaryToDecimal("1111"), 15 )
    test.testEqual( binaryToDecimal("10000"), 16 )
    test.testEqual( binaryToDecimal("10101"), 21 )
    test.testEqual( binaryToDecimal("10110"), 22 )
    test.testEqual( binaryToDecimal("11111"), 31 )
    test.testEqual( binaryToDecimal("100000"), 32 )
    test.testEqual( binaryToDecimal("101011"), 43 )
    test.testEqual( binaryToDecimal("110011"), 51 )
    
    
def binaryToDecimal(binary):
    """
    Converts binary number into a decimal number
    Pre: binaryNum should be a string containing only 0s and 1s
    Post: converts binaryNum into a decimal value and returns that value
    as an integer
    """
    decVal = 0
    for exponent in range(len(binary)):
        index = len(binary) - (exponent + 1)
        char = binary[index]
        if char == "1":
            decVal += 2**exponent
        # Alternative: 
        # bit = int(char)
        # decVal += bit * 2 ** exponent
    
    return decVal
    
testBinaryToDecimal()
#main()

./binaryToDecimal.py 3/4

[
top][prev][next]
# Converts a binary number into a decimal.
# Iterates over the exponents.
# Exercise: implement two ways:
#   --> iterate over the binary string from the last character to the first
#   --> iterate over the binary string from the first character to the last
# By CSCI111

import test

def main():
    print("This program converts a binary number into a decimal number.")
    binNum = input("Enter a binary number: ")
    print(binNum, "has a decimal value of", binaryToDecimal(binNum))


def testBinaryToDecimal():
    """
    tests the binaryToDecimal function
    """
    test.testEqual( binaryToDecimal("0"), 0 )
    test.testEqual( binaryToDecimal("0000"), 0 )
    test.testEqual( binaryToDecimal("1"), 1 )
    test.testEqual( binaryToDecimal("0001"), 1 )
    test.testEqual( binaryToDecimal("10"), 2 )
    test.testEqual( binaryToDecimal("0010"), 2 )
    test.testEqual( binaryToDecimal("111"), 7 )
    test.testEqual( binaryToDecimal("1000"), 8 )
    test.testEqual( binaryToDecimal("1001"), 9 )
    test.testEqual( binaryToDecimal("1101"), 13 )
    test.testEqual( binaryToDecimal("1111"), 15 )
    test.testEqual( binaryToDecimal("10000"), 16 )
    test.testEqual( binaryToDecimal("10101"), 21 )
    test.testEqual( binaryToDecimal("10110"), 22 )
    test.testEqual( binaryToDecimal("11111"), 31 )
    test.testEqual( binaryToDecimal("100000"), 32 )
    test.testEqual( binaryToDecimal("101011"), 43 )
    test.testEqual( binaryToDecimal("110011"), 51 )

    
def binaryToDecimal(binaryNum):
    """
    Converts binary number into a decimal number
    Pre: binaryNum should be a string containing only 0s and 1s
    Post: converts binaryNum into a decimal value and returns that value
    as an integer
    """
    pass # to be implemented ...
    
    

testBinaryToDecimal()
#main()

./decimalToBinary.py 4/4

[
top][prev][next]
# Converts a decimal number into binary
# By CSCI111

import test

def main():
    print("This program converts a decimal number into a binary number.")
    decNum = int(input("Enter a decimal number: "))
    print(decNum, "has a binary value of", decimalToBinary(decNum))


def testDecimalToBinary():
    """
    tests the binaryToDecimal function
    """
    test.testEqual( binaryToDecimal(0), "0" )
    test.testEqual( binaryToDecimal(1), "1" )
    test.testEqual( binaryToDecimal(2), "10" )
    test.testEqual( binaryToDecimal(7), "111" )
    test.testEqual( binaryToDecimal(8), "1000" )
    test.testEqual( binaryToDecimal(9), "1001" )
    test.testEqual( binaryToDecimal(13), "1101" )
    test.testEqual( binaryToDecimal(15), "1111" )
    test.testEqual( binaryToDecimal(16), "10000" )
    test.testEqual( binaryToDecimal(21), "10101" )
    test.testEqual( binaryToDecimal(22), "10110" )
    test.testEqual( binaryToDecimal(31), "11111" )
    test.testEqual( binaryToDecimal(32), "100000" )
    test.testEqual( binaryToDecimal(43), "101011" )
    test.testEqual( binaryToDecimal(51), "110011" )

    
def decimalToBinary(decimal):
    """
    Converts the decimal number (a non-negative integer) into 
    a binary number (as a string)
    """
    
    if decimal == 0:
        return "0"
    
    # Initialize the result to the empty string
    result = ""

    # Repeat until the decimal is 0:
    while decimal != 0:
        result = str(decimal % 2) + result
        
        decimal = decimal // 2

    #Return the result
    return result

    
        

testDecimalToBinary()
#main()

Generated by GNU Enscript 1.6.5.90.