class KnotHashV1: def __init__(self, lengths, list_size=256, initial_position=0, initial_skip_size=0): self.position = initial_position self.skip_size = initial_skip_size self.list = CircularList(list_size) for length in lengths: self.list.reverse(self.position, length) self.position = self.list.get_actual_position(self.position + length + self.skip_size) self.skip_size += 1 def get_current_position(self): return self.position def get_list(self): return self.list.get_list() def get_skip_size(self): return self.skip_size def compute_checksum(self): current_list = self.get_list() return current_list[0] * current_list[1]
def __init__(self, lengths, list_size=256, initial_position=0, initial_skip_size=0): self.position = initial_position self.skip_size = initial_skip_size self.list = CircularList(list_size) for length in lengths: self.list.reverse(self.position, length) self.position = self.list.get_actual_position(self.position + length + self.skip_size) self.skip_size += 1
def __init__(self, message): self.message = message lengths = AsciiConverter.to_ascii_codes(message) position = 0 skip_size = 0 nums = CircularList(256) for _ in range(64): for length in lengths: nums.reverse(position, length) position = nums.get_actual_position(position + length + skip_size) skip_size += 1 sparse_hash = nums.get_list() dense_hash = DenseHashProcessor(sparse_hash).get_hash() self.string_hash = HashFormatter.format(dense_hash)
def testGetActualPosition_whenPositionLT0_shouldRaise(self): sut = CircularList(5) self.assertRaises(Exception, sut.get_actual_position, -1)
def testGetActualPosition_whenPositionLTListLength_shouldReturnGivenPosition( self): sut = CircularList(5) self.assertEqual(4, sut.get_actual_position(position=4))
def testGetActualPosition_whenPositionGTEListLength_shouldReturnGivenPositionModuloListLength( self): sut = CircularList(5) self.assertEqual(0, sut.get_actual_position(position=5))
def testReverse_whenLengthLT0_shouldRaise(self): sut = CircularList(5) self.assertRaises(Exception, sut.reverse, 0, -1)
def testInitialization(self): sut = CircularList(list_size=5) self.assertEqual([0, 1, 2, 3, 4], sut.get_list())
def testReverse_whenPositionGTEListLength_shouldRaise(self): sut = CircularList(5) self.assertRaises(Exception, sut.reverse, 5, 1)
def testReverse_whenPositionLT0_shouldRaise(self): sut = CircularList(5) self.assertRaises(Exception, sut.reverse, -1, 1)
def testReverse_whenLengthEquals0_shouldNotChangeList(self): sut = CircularList(5) sut.reverse(position=0, length=0) self.assertEqual([0, 1, 2, 3, 4], sut.get_list())
def testReverse_whenLengthEqualsListLengthAndPositionGT0_shouldReverseWithWrap( self): sut = CircularList(5) sut.reverse(1, 5) self.assertEqual([1, 0, 4, 3, 2], sut.get_list())
def testReverse_whenPositionPlusLengthGTListLength_shouldReverseWithWrap( self): sut = CircularList(5) sut.reverse(4, 2) self.assertEqual([4, 1, 2, 3, 0], sut.get_list())
def testReverse_whenPositionPlusLengthLTListLength_shouldReverseNoWrap( self): sut = CircularList(5) sut.reverse(1, 3) self.assertEqual([0, 3, 2, 1, 4], sut.get_list())
def testReverse_whenPositionPlusLengthEqualsListLength_shouldReverseFromPositionToListEnd( self): sut = CircularList(5) sut.reverse(1, 4) self.assertEqual([0, 4, 3, 2, 1], sut.get_list())