Beispiel #1
0
    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)), [])
Beispiel #2
0
    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))
Beispiel #3
0
    def testRemoveDuplicatedInSortedListWithTwoDuplicateLinks(self):
        testHead = Link(2)
        firstLink = Link(2)
        testHead.nextLink = firstLink

        expectedHead = removeDuplicatesInSortedList(testHead)
        self.assertEqual(self.linkedListToArray(expectedHead), [2])
Beispiel #4
0
    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 testPartitionWithDoubleNode(self):
        testHead = Link(10)
        firstLink = Link(4)

        testHead.nextLink = firstLink

        expectedHead = partition(testHead, 5)
        self.assertEqual([4, 10], self.linkedListToArray(expectedHead))
Beispiel #6
0
    def testDeleteMiddleWithSingleLink(self):
        testHead = Link(2)
        firstLink = Link(4)

        testHead.nextLink = firstLink

        deleteMiddleNode(firstLink)
        self.assertEqual([2, 4], self.linkedListToArray(testHead))
    def testLoopDetectionWithValidCase(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
        seventhLink.nextLink = testHead

        self.assertEqual(loopDetection(testHead), testHead)
    def testIntersectionWithValidCase(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(2)
        testHeadTwo.nextLink = thirdLink

        self.assertTrue(intersection(testHeadOne, testHeadTwo))
Beispiel #9
0
    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)
Beispiel #11
0
    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])
Beispiel #13
0
    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 testKthToLastWithTwoLinks(self):
     testHead = Link(3)
     firstLink = Link(5)
     testHead.nextLink = firstLink
     expectedResult = kthToLast(testHead, 1)
     self.assertEqual(expectedResult, firstLink)
 def testKthToLastWithZeroIndex(self):
     testHead = Link(3)
     firstLink = Link(5)
     testHead.nextLink = firstLink
     expectedResult = kthToLast(testHead, 0)
     self.assertEqual(expectedResult, None)