def binaryToNum(s): '''Precondition: s is a string of 0s and 1s. Returns the integer corresponding to the binary representation in s. Note: the empty string represents 0.''' if s == '': return 0 elif int(s[-1]) == 1: return 1 + 2 * (binaryToNum(s[:-1])) else: return 2 * binaryToNum(s[:-1])
def test03(self): self.assertEqual(lab6.binaryToNum(''), 0) self.assertEqual(lab6.binaryToNum('0'), 0) self.assertEqual(lab6.binaryToNum('1'), 1) self.assertEqual(lab6.binaryToNum('100'), 4) self.assertEqual(lab6.binaryToNum('1011'), 11) self.assertEqual(lab6.binaryToNum('00001011'), 11) self.assertEqual(lab6.binaryToNum('101010'), 42) self.assertEqual(lab6.binaryToNum('1100100'), 100) self.assertEqual(lab6.binaryToNum('1001110111101000111010'), 2587194)
def uncompress_helper(C, count): if C == '': return '' return binaryToNum( C[:COMPRESSED_BLOCK_SIZE]) * count + uncompress_helper( C[COMPRESSED_BLOCK_SIZE:], str(1 - int(count)))