def test_greater_than(self): a = sparse_list.SparseList([1, 2, 3, 0]) b = sparse_list.SparseList([1, 2, 4, 5]) self.assertTrue(b > a) self.assertFalse(b == a) self.assertFalse(b <= a) self.assertFalse(b < a)
def test_concatenation(self): a = sparse_list.SparseList([1, 2, 3]) b = sparse_list.SparseList([4, 5, 6]) c = a + b self.assertEquals([1, 2, 3], a) self.assertEquals([4, 5, 6], b) self.assertEquals([1, 2, 3, 4, 5, 6], c)
def test_less_than(self): a = sparse_list.SparseList([1, 2, 3, 0]) b = sparse_list.SparseList([1, 2, 4, 5]) self.assertTrue(a < b) self.assertFalse(a == b) self.assertFalse(a >= b) self.assertFalse(a > b)
def test_inequality_left_longer(self): a = sparse_list.SparseList([1, 2, 3, 4]) b = sparse_list.SparseList([1, 2, 3]) self.assertTrue(a != b) self.assertTrue(not (a == b)) self.assertNotEqual(a, b) self.assertTrue(b != a) self.assertTrue(not (b == a)) self.assertNotEqual(b, a)
def test_inequality_same_length(self): a = sparse_list.SparseList([1, 2, 3]) b = sparse_list.SparseList([1, 0, 3]) self.assertTrue(a != b) self.assertTrue(not a == b) self.assertNotEqual(a, b) self.assertTrue(b != a) self.assertTrue(not b == a) self.assertNotEqual(b, a)
def test_equality(self): a = sparse_list.SparseList([1, 2, 3]) b = sparse_list.SparseList([1, 2, 3]) self.assertTrue(a == b) self.assertTrue(not a != b) self.assertEquals(a, b) self.assertTrue(b == a) self.assertTrue(not b != a) self.assertEquals(b, a)
def test_multiply(self): sl = sparse_list.SparseList({0: 1, 4: 1}, 0) sl4 = sl * 4 self.assertEquals([1, 0, 0, 0, 1], sl) self.assertEquals( [1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1], sl4) self.assertEquals(len(sl) * 4, len(sl4))
def __init__(self, fname): self.program = sparse_list.SparseList(0x100000000) page = 0 with open(fname, "r") as f: for s in f: if s[0] == ":": # good - it starts sensibly... numbers = bytearray.fromhex(s.strip()[1:]) record_length = numbers[0] offset = numbers[1] * 256 + numbers[2] rectype = numbers[3] data = numbers[4:-1] checksum = reduce(operator.add, numbers) & 0xff if (checksum != 0) or (record_length != len(data)): raise HexFileError("Bad checksum") else: raise HexFileError("Bad format") # so far, so good. Now to slot the data in... if rectype not in (0, 1, 4): # rectype not known. Fall over raise HexFileError("Unknown record format") if rectype == 0: self.program[page + offset:page + offset + record_length] = data if rectype == 1: # end of file return if rectype == 4: if offset != 0: # really should be zero... raise HexFileError("Bad format") if record_length != 2: # incorrect length raise HexFileError("Bad format") page = data[0] * 0x1000000 + data[1] * 0x10000 page = page | 0x80000000
def test_extended_slice_with_negative_stop(self): sl = sparse_list.SparseList([ 0, 1, 2, 3, 4, 5, 6, ]) self.assertEquals([1, 3, 5], sl[1:-1:2])
def test_extended_slice(self): sl = sparse_list.SparseList([ 0, 1, 2, 3, 4, 5, 6, ]) self.assertEquals([1, 3, 5], sl[1:6:2])
def test_access_with_negative_index_with_no_value(self): sl = sparse_list.SparseList(5, 0) self.assertEquals(0, sl[-1])
def test_init_zero(self): sl = sparse_list.SparseList(0) self.assertEquals(0, len(sl))
def test_initialisation_by_list(self): sl = sparse_list.SparseList([0, 1, 2, 4]) self.assertEquals([0, 1, 2, 4], sl)
def test_string_representations(self): sl = sparse_list.SparseList(5, 0) sl[3], sl[4] = 5, 6 self.assertEquals('[0, 0, 0, 5, 6]', repr(sl)) self.assertEquals('[0, 0, 0, 5, 6]', str(sl))
def test_membership_absent(self): sl = sparse_list.SparseList(5) sl[2], sl[3], = 1, 2 self.assertEquals(False, 3 in sl)
def test_iteration_empty(self): sl = sparse_list.SparseList(3) self.assertEquals([None, None, None], list(sl))
def test_iteration_populated(self): sl = sparse_list.SparseList(5) sl[1], sl[3] = 'a', 'b' self.assertEquals([None, 'a', None, 'b', None], list(sl))
def test_random_access_read_present(self): sl = sparse_list.SparseList(2) sl[0] = 'brent' self.assertEquals('brent', sl[0])
def test_membership_present(self): sl = sparse_list.SparseList(5) sl[2], sl[3], = 1, 2 self.assertEquals(True, 2 in sl)
def test_push_value(self): sl = sparse_list.SparseList([1, 2, 3]) sl.push(4) self.assertEquals(4, len(sl)) self.assertEquals([1, 2, 3, 4], sl)
def test_initialisation_by_dict(self): sl = sparse_list.SparseList({ 4: 6, 3: 5, }, 0) self.assertEquals([0, 0, 0, 5, 6], sl)
def test_remove_value(self): sl = sparse_list.SparseList([1, 2, 3]) sl.remove(2) self.assertEquals(3, len(sl)) self.assertEquals([1, None, 3], sl)
def test_initialisation_by_generator(self): gen = (x for x in (1, 2, 3)) sl = sparse_list.SparseList(gen) self.assertEquals([1, 2, 3], sl)
def test_remove_only_first_value(self): sl = sparse_list.SparseList([2, 2, 3]) sl.remove(2) self.assertEquals(3, len(sl)) self.assertEquals([None, 2, 3], sl)
def test_access_with_negative_index(self): sl = sparse_list.SparseList([0, 1, 2, 4]) self.assertEquals(4, sl[-1])
def test_remove_non_value(self): sl = sparse_list.SparseList([1, 2, 3]) self.assertRaises(ValueError, sl.remove, 4)
def test_slice(self): sl = sparse_list.SparseList([0, 1, 2, 4], 10) self.assertEquals([1, 2], sl[1:3])
def test_remove_default_value_does_nothing(self): sl = sparse_list.SparseList(4, None) sl.remove(None) self.assertEquals([None, None, None, None], sl)
def _calc_f_finetune(self, L, new_seqs, n, remaining_m): global count9 global non9 prefix = sparse_list.SparseList(0, 9) new_seqs_incremental = [] ncrl = L-1 self.nrange = range(ncrl+1) cant_start_with_zero = 1 zrange = range(cant_start_with_zero, 9) digsum = 0 def merge(dest_seq_by_len): sequences = [_get_initial_0s() for _ in range(L-len(prefix)+1)] for s in dest_seq_by_len: for ll, digits in enumerate(s): for dd, v in enumerate(digits): sequences[ll][dd] += v return sequences for news in new_seqs: new_dest_seq_by_len = [] for ll, newx in enumerate(news): if ll == 0: continue new_dest_len_seq__by_digit = _get_initial_0s() for dd, y in enumerate(newx): new_dest_len_seq__by_digit[dd] += y new_dest_seq_by_len.append(new_dest_len_seq__by_digit) new_seqs_incremental.append(new_dest_seq_by_len) self._set_seq(new_seqs_incremental[1]) def descend_several_9s(count_new_9s): nonlocal digsum nonlocal ncrl prefix.size += count_new_9s digsum += count_new_9s * 9 ncrl -= count_new_9s self.nrange = range(ncrl+1) def descend(d): nonlocal cant_start_with_zero nonlocal digsum nonlocal ncrl if cant_start_with_zero: cant_start_with_zero = 0 nonlocal zrange zrange = self.range9 new_dest_seq_by_len = merge(new_seqs_incremental) self._set_seq(new_dest_seq_by_len) prefix.append(d) digsum += d ncrl -= 1 self.nrange = range(ncrl+1) # pprint.pprint([prefix.elements, prefix.size]) if d < 9: new_dest_seq_by_len = self._gen_L_initial_sequences( L-len(prefix), n-digsum) new_sequences = merge(new_dest_seq_by_len) new_sequences.pop(0) self._set_seq(new_sequences) while True: # pprint.pprint(locals()) def count_perms(ll, lenitem): def _c(): if lenitem == 0: return 1 if lenitem == 1: return ncrl return nCr(ncrl, lenitem) if ll: return _c() * ll return 0 remainlen = L - len(prefix) assert remainlen >= 0 maxdigsum = remainlen * 9 + digsum digitcond = maxdigsum == n+1 if maxdigsum == n or digitcond: if digitcond: prefix.size += (remaining_m-1) prefix.append(8) prefix.size += (remainlen - remaining_m) else: prefix.size += remainlen assert prefix.size == L keys = sorted(prefix.elements.keys()) reached = 0 ret = 0 def _advance_reached(place): nonlocal reached nonlocal ret if reached >= place: return mat = self.mat_exp_mod(place - reached) ret = (mat[1][0] + ret * mat[1][1]) % self.MOD reached = place for place in keys: _advance_reached(place) ret = (ret * 10 + prefix[place]) % self.MOD reached += 1 _advance_reached(prefix.size) # ret = ''.join(prefix) return int(ret) mydiff8 = sum( count_perms( self._new_seq_counts[i], i, ) for i in self.nrange ) if mydiff8 < remaining_m: use_one = True if len(prefix) and remainlen: def _count_total(remain_digsum, suffix_len): key = (remain_digsum, suffix_len) if key in self._count_total_cache: print_('hit', key) return self._count_total_cache[key] ret = self._gen_01_L_initial_sequences( suffix_len, remain_digsum, ) self._count_total_cache[key] = ret return ret expected_before = _count_total( n - digsum, remainlen ) if 0: got = sum([self._new_seq_counts[i]*nCr(remainlen, i+1) for i in range(min([ 1+remainlen, len(self._new_seq_counts)]))]) compare(expected=got, got=expected_before, blurb="_new_seq_counts") top = remainlen bottom = 1 while use_one and (top-bottom > 1): remainlen_to_check = (top+bottom) >> 1 count_new_9s = remainlen - remainlen_to_check new_digsum = digsum + count_new_9s * 9 new_remain_digsum = n - new_digsum if new_remain_digsum <= 0: top = remainlen_to_check-1 else: expected_after = _count_total( new_remain_digsum, remainlen_to_check) expected = expected_before - expected_after if expected < remaining_m: remaining_m -= expected descend_several_9s(count_new_9s) print_('count_new_9s', count_new_9s) use_one = False else: bottom = remainlen_to_check + 1 # print_('new bottom =', bottom, expected) if use_one: remaining_m -= mydiff8 descend(9) count9 += 1 # print_('count9 =', count9, 'non9 =', non9) else: non9 += 1 for d in zrange: mydiff = sum( count_perms(self.newzs[i][d], i) for i in self.nrange ) if mydiff < remaining_m: remaining_m -= mydiff else: descend(d) break
def test_random_access_read_absent(self): sl = sparse_list.SparseList(2, 'absent') sl[1] = 'clint' self.assertEquals('absent', sl[0])