def testRemoveDuplicatedInSortedListWithTwoDuplicateLinks(self): testHead = Link(2) firstLink = Link(2) testHead.nextLink = firstLink expectedHead = removeDuplicatesInSortedList(testHead) self.assertEqual(self.linkedListToArray(expectedHead), [2])
def testPartitionWithDoubleNode(self): testHead = Link(10) firstLink = Link(4) testHead.nextLink = firstLink expectedHead = partition(testHead, 5) self.assertEqual([4, 10], self.linkedListToArray(expectedHead))
def testDeleteMiddleWithSingleLink(self): testHead = Link(2) firstLink = Link(4) testHead.nextLink = firstLink deleteMiddleNode(firstLink) self.assertEqual([2, 4], self.linkedListToArray(testHead))
def testSumLinkedListWithNoneValue(self): secondNumberDigitOne = Link(5) secondNumberDigitSecond = Link(9) secondNumberDigitThree = Link(2) secondNumberDigitOne.nextLink = secondNumberDigitSecond secondNumberDigitSecond.nextLink = secondNumberDigitThree self.assertEqual(self.linkedListToArray(sumLinkedLists(None, None)), [])
def testDeleteMiddleLink(self): testHead = Link(2) firstLink = Link(4) secondLink = Link(5) thirdLink = Link(7) fourthLink = Link(9) testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink deleteMiddleNode(secondLink) self.assertEqual([2, 4, 7, 9], self.linkedListToArray(testHead))
def testIntersectionWithInvalidCase(self): testHeadOne = Link(3) firstLink = Link(5) secondLink = Link(10) thirdLink = Link(11) fourthLink = Link(23) fifthLink = Link(1) sixthLink = Link(42) seventhLink = Link(9) testHeadOne.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink sixthLink.nextLink = seventhLink testHeadTwo = Link(3) firstLinkTwo = Link(3) testHeadTwo.nextLink = firstLinkTwo self.assertFalse(intersection(testHeadOne, testHeadTwo))
def testLoopDetectionWithInvalidCase(self): testHead = Link(3) firstLink = Link(5) secondLink = Link(10) thirdLink = Link(11) fourthLink = Link(23) fifthLink = Link(1) sixthLink = Link(42) seventhLink = Link(9) testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink sixthLink.nextLink = seventhLink self.assertEqual(loopDetection(testHead), testHead)
def testSumLinkedListWithLargeDigit(self): firstNumberDigitOne = Link(7) firstNumberDigitSecond = Link(1) firstNumberDigitThree = Link(1) firstNumberDigitFour = Link(6) firstNumberDigitFive = Link(6) firstNumberDigitSix = Link(6) firstNumberDigitOne.nextLink = firstNumberDigitSecond firstNumberDigitSecond.nextLink = firstNumberDigitThree firstNumberDigitThree.nextLink = firstNumberDigitFour firstNumberDigitFour.nextLink = firstNumberDigitFive firstNumberDigitFive.nextLink = firstNumberDigitSix secondNumberDigitOne = Link(5) secondNumberDigitSecond = Link(9) secondNumberDigitThree = Link(2) secondNumberDigitOne.nextLink = secondNumberDigitSecond secondNumberDigitSecond.nextLink = secondNumberDigitThree self.assertEqual(self.linkedListToArray(sumLinkedLists(firstNumberDigitOne, secondNumberDigitOne)), [6, 6, 6, 4, 1, 2])
def testRemoveDuplicatesInSortedList(self): testHead = Link(1) firstLink = Link(2) secondLink = Link(2) thirdLink = Link(2) fourthLink = Link(3) fifthLink = Link(3) sixthLink = Link(4) seventhLink = Link(4) testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink sixthLink.nextLink = seventhLink expectedHead = removeDuplicatesInSortedList(testHead) self.assertEqual(self.linkedListToArray(expectedHead), [1, 2, 3, 4])
def testKthToLast(self): testHead = Link(3) firstLink = Link(5) secondLink = Link(10) thirdLink = Link(11) fourthLink = Link(23) fifthLink = Link(1) sixthLink = Link(42) seventhLink = Link(9) testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink sixthLink.nextLink = seventhLink expectedResult = kthToLast(testHead, 3) self.assertEqual(expectedResult, fifthLink)
def testPalindromeWithValidCase(self): testHead = Link("r") firstLink = Link("a") secondLink = Link("c") thirdLink = Link("e") fourthLink = Link("c") fifthLink = Link("a") sixthLink = Link("r") testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink self.assertEqual(palindrome(testHead), True)
def testPartition(self): testHead = Link(3) firstLink = Link(5) secondLink = Link(8) thirdLink = Link(5) fourthLink = Link(10) fifthLink = Link(2) sixthLink = Link(1) testHead.nextLink = firstLink firstLink.nextLink = secondLink secondLink.nextLink = thirdLink thirdLink.nextLink = fourthLink fourthLink.nextLink = fifthLink fifthLink.nextLink = sixthLink expectedHead = partition(testHead, 5) self.assertEqual(self.linkedListToArray(expectedHead), [1, 2, 3, 5, 8, 5, 10])
def testBaseSumLinkedLists(self): firstNumberDigitOne = Link(7) firstNumberDigitSecond = Link(1) firstNumberDigitThree = Link(6) firstNumberDigitOne.nextLink = firstNumberDigitSecond firstNumberDigitSecond.nextLink = firstNumberDigitThree secondNumberDigitOne = Link(5) secondNumberDigitSecond = Link(9) secondNumberDigitThree = Link(2) secondNumberDigitOne.nextLink = secondNumberDigitSecond secondNumberDigitSecond.nextLink = secondNumberDigitThree self.assertEqual(self.linkedListToArray(sumLinkedLists(firstNumberDigitOne, secondNumberDigitOne)), [9, 1, 2])
def testRemoveDuplicatedInSortedListWithSingleLink(self): testHead = Link(2) expectedHead = removeDuplicatesInSortedList(testHead) self.assertEqual(self.linkedListToArray(expectedHead), [2])
def testPartitionWithSingleNode(self): testHead = Link(5) expectedHead = partition(testHead, 5) self.assertEqual(self.linkedListToArray(expectedHead), [5])
def testKthToLastWithSingleLink(self): testHead = Link(3) expectedResult = kthToLast(testHead, 3) self.assertEqual(expectedResult, testHead)
def testPalindromeWithSingleCharacter(self): self.assertEqual(palindrome(Link("a")), True)
def testKthToLastWithZeroIndex(self): testHead = Link(3) firstLink = Link(5) testHead.nextLink = firstLink expectedResult = kthToLast(testHead, 0) self.assertEqual(expectedResult, None)
def testKthToLastWithTwoLinks(self): testHead = Link(3) firstLink = Link(5) testHead.nextLink = firstLink expectedResult = kthToLast(testHead, 1) self.assertEqual(expectedResult, firstLink)