def test_append(self): a = range(10) b = range(11) sll = SinglyLinkedList(a) self.assertEquals(list(sll), a) sll.append(10) self.assertEquals(list(sll), b)
def add(left, right, msd=False): """Set msd to True if left and right are in "most significant digit first" order. The result will be in the same order as the input.""" if msd: left = left.reverse() right = right.reverse() result = SinglyLinkedList() carry = 0 a = left.head b = right.head while a and b: c = a.payload + b.payload result.append(carry + c % 10) carry = c / 10 a = a.next_ b = b.next_ while a: result.append(a.payload) a = a.next_ while b: result.append(b.payload) b = b.next_ if msd: return result.reverse() return result
def partition(sll, x): lt = SinglyLinkedList() geq = SinglyLinkedList() for value in sll: if value < x: lt.append(value) else: geq.append(value) return lt + geq
def setUp(self): self.sll = SinglyLinkedList("abcde") self.sll.tail.next_ = self.sll.head.next_.next_ self.start_of_loop = self.sll.tail.next_ # # Technically, our SinglyLinkedList isn't suitable for solving this # problem, since it has a tail pointer, and circular lists cannot # have a tail. However, it's "good enough" to demonstrate the # solution, since the solution never uses the tail pointer. # self.sll.tail = None
def test_no_loop_clever(self): self.assertEquals(start_of_loop2(SinglyLinkedList("misha")), None)
def test_sanity(self): self.assertEquals(is_palindrome(SinglyLinkedList("x")), True) self.assertEquals(is_palindrome(SinglyLinkedList("radar")), True) self.assertEquals(is_palindrome(SinglyLinkedList("sonar")), False)
def test_empty(self): self.assertEquals(is_palindrome(SinglyLinkedList("")), True)
def test_add(self): a1 = range(10) a2 = range(100, 110) sll = SinglyLinkedList(a1) + SinglyLinkedList(a2) self.assertEquals(list(sll), a1 + a2)
def test_empty(self): sll = SinglyLinkedList() self.assertEquals(list(sll), []) sll = partition(sll, 10) self.assertEquals(list(sll), [])
def test_diff_lsd(self): a = SinglyLinkedList([1, 2, 3]) b = SinglyLinkedList([2]) c = [3, 2, 3] self.assertEquals(list(add(a, b)), c)
def test_lsd(self): a = SinglyLinkedList([7, 1, 6]) b = SinglyLinkedList([5, 9, 2]) c = [2, 1, 9] self.assertEquals(list(add(a, b)), c)
def setUp(self): self.sll = SinglyLinkedList("abcde")
def worker(self, dup, expected, fn): sll = SinglyLinkedList(dup) self.assertNotEquals(list(sll), expected) self.assertEquals(list(fn(sll)), expected)
def test_append_empty(self): sll = SinglyLinkedList() self.assertEquals(list(sll), []) sll.append(1) self.assertEquals(list(sll), [1])
from sll import SinglyLinkedList l = SinglyLinkedList() lst = [1, 5, 2, 6, 3, 2, 1, 1, 5, 3] for i in lst: l.addTail(i) def removeDup(sll): """ remove duplicates from a singly linked list """ if sll.head is None: # empty linked list return walk = sll.head store = set() store.add(walk.data) while walk.next != None: if walk.next.data not in store: store.add(walk.next.data) walk = walk.next else: walk.next = walk.next.next print(store) p = sll.head while p != None: print(p.data) p = p.next
def test_empty(self): l = [] sll = SinglyLinkedList(l) self.assertEquals(l, list(sll)) self.assertEquals(len(l), 0)
def test_empty(self): sll = SinglyLinkedList() self.assertEquals(list(sll), [])
def test_sanity(self): a = range(10) sll = SinglyLinkedList(a) self.assertEquals(list(sll.reverse()), [x for x in reversed(a)])
def test_msd(self): a = SinglyLinkedList([6, 1, 7]) b = SinglyLinkedList([2, 9, 5]) c = [9, 1, 2] self.assertEquals(list(add(a, b, True)), c)
def test_sanity(self): sll = SinglyLinkedList([1, 2, 1, 3, 2, 0]) sll = partition(sll, 2) self.assertEquals(list(sll), [1, 1, 0, 2, 3, 2])
def test_diff_msd(self): a = SinglyLinkedList([1, 2, 3]) b = SinglyLinkedList([2]) c = [1, 2, 5] self.assertEquals(list(add(a, b, True)), c)
def test_sanity2(self): sll = SinglyLinkedList(range(50, 60) + range(10)) sll = partition(sll, 10) self.assertEquals(list(sll), range(10) + range(50, 60))
def setUp(self): self.empty = SinglyLinkedList() self.sll = SinglyLinkedList([1, 2, 3])
def test_sanity(self): l = range(10) sll = SinglyLinkedList(l) self.assertEquals(l, list(sll)) self.assertEquals(len(l), 10)