# William M. Farmer and Spencer Smith
# baseCovert.py
# 27 January 2012

# Base 2 to base 10 conversion

a = [1, 0, 1, 1]

# expanded (naive) summation (wrong result)
num = a[3]*2**3 + a[2]*2**2 + a[1]*2**1 + a[0]*2**0
print
print "Using an explicit (naive) expansion of the summation."
print num
print int("1011", 2)

# expanded summation
num = a[0]*2**3 + a[1]*2**2 + a[2]*2**1 + a[3]*2**0
print
print "Using an explicit expansion of the summation."
print num
print int("1011", 2)

# using a (naive) loop (wrong result)
num = 0
for k in range(4):
    num = num + a[k]*2**k
print
print "Using a (naive) loop for the summation."
print num
print int("1011", 2)

# using a loop (correct result)
num = 0
for k in range(4):
    num = num + a[3 - k]*2**k
print
print "Using a loop for the summation."
print num
print int("1011", 2)

# using a general loop that converts any list representing a number in base 2
num = 0
for k in range(len(a)):
    num = num + a[len(a) - 1 - k]*2**k
print
print "Using a general loop."
print num
print int("1011", 2)
