def bits_to_int(bits): total = 0 pow2 = 1 for bit in bits: total += bit * pow2 pow2 *= 2 return total def int_to_bits(num): if num == 0: return [0] else: bits = [] while num > 0: last_bit = num % 2 bits.append(last_bit) num = num / 2 return bits def digits_to_int(digits, base): # 1. Remove the line below # 2. Copy the code from bits_to_int into this function # 3. Replace the 2s with base return 0 def int_to_digits(num, base): # 1. Remove the line below # 2. Copy the code from int_to_bits into this function # 3. Replace all the 2s with base return [] def bit_string_to_int(bit_string): # 0. Remove the line of code below! # 1. call list(bit_string), and put the result in a variable called # bit_strings. # bit_strings is a list of strings. Each string is either '0' or '1' # 2. create another list, called bits, which will contain a list of ints # either 0 or 1. Initially, make that list empty : [] # 3. write a for-loop which visits each string in bit_strings. # each string should be converted into an int, # and the result whould be appended to bits # 4. you will need to bits.reverse() , because the bits are backwards: # the most significant bit should be LAST in the list, not FIRST. # 5. call bits_to_int(bits), and return its result. return 0 def pair_averages(numbers): # This function should compute the averages of pairs of numbers, # but it doesn't work correctly. for i in range(1, len(numbers)): numbers[i] = (numbers[i - 1] + numbers[i]) / 2 return numbers def main(): print 'digits_to_int([1,2,3,0], 10)=',digits_to_int([1,2,3,0], 10) print 'int_to_digits(1230, 10)=',int_to_digits(1230, 10) print "bit_string_to_int('11011')=",bit_string_to_int('11011') print 'pair_averages([10,20,30,40])=',pair_averages([10,20,30,40]) if __name__ == '__main__': main()