示例#1
0
    def test_length_of_list_after_list_change_operations(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(10)
        singlyLinkedListObject.populate(inputList)

        expectedLength = len(inputList)

        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))

        singlyLinkedListObject.deleteHead()

        expectedLength -= 1

        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))

        singlyLinkedListObject.insertAtHead(200)
        expectedLength += 1

        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))


        singlyLinkedListObject.insertAtHead(200)
        expectedLength += 1

        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))
    def method1(self):
        '''
        1. Go to the centre of the list using the slow and fast pointer approach
        2. Create another reversed list starting from the second half
        3. Compare this new list with the original list head and stop when the new list hits None
        :return: Boolean
        '''
        centreOfLinkedList = self.singlyLinkedListObject.returnCentreOfTheLinkedList()
        if centreOfLinkedList is None:
            # print "Empty List is a palindrome"
            return True

        newHalfList = SinglyLinkedList()
        newHalfList.populateNewListStartingFromALinkedListNode(centreOfLinkedList.nextPointer)

        #Reversing
        ReverseASinglyLinkedList.Solution().reverseOriginalList_Iterative(newHalfList)

        originalListCurrentNode = self.singlyLinkedListObject.head
        for dataFromSecondHalf in newHalfList:
            if originalListCurrentNode.data != dataFromSecondHalf:
                # print "Not a palindrome"
                return False
            originalListCurrentNode = originalListCurrentNode.nextPointer

        # print "Linked List is a palindrome"
        return True
示例#3
0
    def method1(self):
        '''
        1. Go to the centre of the list using the slow and fast pointer approach
        2. Create another reversed list starting from the second half
        3. Compare this new list with the original list head and stop when the new list hits None
        :return: Boolean
        '''
        centreOfLinkedList = self.singlyLinkedListObject.returnCentreOfTheLinkedList(
        )
        if centreOfLinkedList is None:
            # print "Empty List is a palindrome"
            return True

        newHalfList = SinglyLinkedList()
        newHalfList.populateNewListStartingFromALinkedListNode(
            centreOfLinkedList.nextPointer)

        #Reversing
        ReverseASinglyLinkedList.Solution().reverseOriginalList_Iterative(
            newHalfList)

        originalListCurrentNode = self.singlyLinkedListObject.head
        for dataFromSecondHalf in newHalfList:
            if originalListCurrentNode.data != dataFromSecondHalf:
                # print "Not a palindrome"
                return False
            originalListCurrentNode = originalListCurrentNode.nextPointer

        # print "Linked List is a palindrome"
        return True
示例#4
0
    def test_list_as_generator(self):

        singlyLinkedListObject = SinglyLinkedList()
        inputList = range(10,100,10)
        singlyLinkedListObject.populate(inputList)
        for index, data in enumerate(singlyLinkedListObject):
            self.assertTrue(data == inputList[index], "Data mismatch. Expected data: %s, Got: %s" %(inputList[index], data))
    def test_reverse_a_list_with_two_elements(self):
        givenList = SinglyLinkedList()
        inputList = [10, 20]
        givenList.populate(inputList)

        s = Solution()

        s.printReverseUsingRecursion(givenList.head)
示例#6
0
    def test_populate(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(20)
        singlyLinkedListObject.populate(inputList)
        outputList = singlyLinkedListObject.returnLinkedListAsList()

        self.assertTrue(inputList == outputList, "Input List: %s and Output List: %s don't match" %(inputList, outputList))
示例#7
0
    def test_append_when_list_is_empty(self):
        singlyLinkedListObject = SinglyLinkedList()
        inputVal = 10
        singlyLinkedListObject.appendToListUsingTail(inputVal)
        outputList = singlyLinkedListObject.returnLinkedListAsList()
        outputVal = outputList[0]

        self.assertTrue(inputVal == outputVal, "Expected a tail value of: %s, Got: %s" %(inputVal, outputVal))
    def test_reverse_a_list_with_many_elements(self):
        givenList = SinglyLinkedList()
        inputList = range(10,101,10)
        givenList.populate(inputList)

        s = Solution()

        s.printReverseUsingRecursion(givenList.head)
    def test_yield_reverse_list_with_three_elements(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = [1, 2, 3]
        singlyLinkedListObject.populate(inputList)

        s = Solution()

        for i in s.yieldListDataInReversedOrderUsingRecursion(singlyLinkedListObject.head):
            print i
    def test_reverse_an_empty_list(self):
        givenList = SinglyLinkedList() # Because it's an empty list, no need to populate it

        s = Solution()
        outputLinkedList = SinglyLinkedList()
        s.returnReversedListUsingRecursion(givenList.head, outputLinkedList)
        inputList = []
        expectedList = inputList
        returnedList = outputLinkedList.returnLinkedListAsList()
        self.assertTrue(expectedList == returnedList, "Expected: %s, Got: %s" %(expectedList, returnedList))
    def test_reverse_an_empty_list(self):
        originalList = SinglyLinkedList() # Because it's an empty list, no need to populate it

        s = Solution()
        s.reverseOriginalList_Iterative(originalList)

        inputList = []
        expectedList = inputList
        returnedList = originalList.returnLinkedListAsList()
        self.assertTrue(expectedList == returnedList, "Expected: %s, Got: %s" %(expectedList, returnedList))
示例#12
0
    def test_delete_head_of_empty_list_more_than_once(self):
        singlyLinkedListObject = SinglyLinkedList()

        singlyLinkedListObject.deleteHead()
        self.assertTrue(singlyLinkedListObject.isHeadNone(), "Head of empty list must be None")

        singlyLinkedListObject.deleteHead()
        self.assertTrue(singlyLinkedListObject.isHeadNone(), "Head of empty list must be None")

        singlyLinkedListObject.deleteHead()
        self.assertTrue(singlyLinkedListObject.isHeadNone(), "Head of empty list must be None")
示例#13
0
    def test_delete_head_of_list_with_one_item(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = [1]
        singlyLinkedListObject.populate(inputList)

        singlyLinkedListObject.deleteHead()
        self.assertTrue(singlyLinkedListObject.isHeadNone(), "Now that the only element in the list is deleted, Head of empty list must be None")

        singlyLinkedListObject.deleteHead()
        self.assertTrue(singlyLinkedListObject.isHeadNone(), "Now that the only element in the list is deleted, Head of empty list must be None")
示例#14
0
    def test_return_centre_of_list_with_many_even_elements(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(40000)
        singlyLinkedListObject.populate(inputList)

        centreIndex = (len(inputList)-1)/2
        expectedCentre = inputList[centreIndex]

        actualCentre = singlyLinkedListObject.returnCentreOfTheLinkedList()
        self.assertTrue(expectedCentre == actualCentre.data, "Expected centre if linked list: %s, Got: %s" %(expectedCentre, actualCentre))
示例#15
0
    def test_length_of_list_with_many_elements(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(100000)
        singlyLinkedListObject.populate(inputList)

        expectedLength = len(inputList)

        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))
    def test_add_without_carry_basic_case_both_lists_empty(self):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()
        outputList = SinglyLinkedList()

        s = Solution(firstList, secondList, outputList)
        s.addTwoNonReversedIntegersAsLinkedListsAndCreateNewList()

        outputValue = outputList.returnLinkedListAsList()
        expectedValue = []

        self.assertTrue(outputValue == expectedValue, "Expected: %s, Got: %s" %(expectedValue, outputValue))
    def test_returnNodeAtBeginingOfLinkedListLoop(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(1,12)
        singlyLinkedListObject.populate(inputList)

        s = Solution(singlyLinkedListObject)

        # Insert a loop at 4th position
        singlyLinkedListObject.tail.nextPointer = singlyLinkedListObject.head.nextPointer.nextPointer.nextPointer

        s.returnNodeAtBeginingOfLinkedListLoop()
示例#18
0
    def test_returnNodeAtBeginingOfLinkedListLoop(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(1, 12)
        singlyLinkedListObject.populate(inputList)

        s = Solution(singlyLinkedListObject)

        # Insert a loop at 4th position
        singlyLinkedListObject.tail.nextPointer = singlyLinkedListObject.head.nextPointer.nextPointer.nextPointer

        s.returnNodeAtBeginingOfLinkedListLoop()
    def test_reverse_a_list_with_two_elements(self):
        originalList = SinglyLinkedList()
        inputList = [10, 20]
        originalList.populate(inputList)

        s = Solution()

        s.reverseOriginalLinkedList_Recursive(originalList)

        expectedList = inputList[::-1]
        returnedList = originalList.returnLinkedListAsList()
        self.assertTrue(expectedList == returnedList, "Expected: %s, Got: %s" %(expectedList, returnedList))
    def test_list_with_one_value(self):
        singlyLinkedList = SinglyLinkedList()

        inputList = [1]
        singlyLinkedList.populate(inputList)

        s = Solution(singlyLinkedList)

        expectedValue = True  # Considering empty list is a palindrome
        receivedValue = s.method2()
        self.assertTrue(
            expectedValue == receivedValue,
            "Expected: %s, Received: %s" % (expectedValue, receivedValue))
    def test_add_without_carry_basic_case_second_list_empty_first_list_has_more_than_one_element(self):
        firstList = SinglyLinkedList()
        inputValue = range(10)
        firstList.populate(inputValue)

        secondList = SinglyLinkedList()

        s = Solution(firstList, secondList)

        returnLinkedList = s.addTwoReversedIntegersAsLinkedListsAndCreateNewList()
        outputValue = returnLinkedList.returnLinkedListAsList()
        expectedValue = inputValue
        self.assertTrue(outputValue == expectedValue, "Expected: %s, Got: %s" %(expectedValue, outputValue))
示例#22
0
    def test_add_without_carry_basic_case_both_lists_empty(self):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()

        s = Solution(firstList, secondList)

        returnLinkedList = s.addTwoReversedIntegersAsLinkedListsAndCreateNewList(
        )
        outputValue = returnLinkedList.returnLinkedListAsList()
        expectedValue = []

        self.assertTrue(outputValue == expectedValue,
                        "Expected: %s, Got: %s" % (expectedValue, outputValue))
    def test_list_with_four_values_not_a_palindrome(self):
        singlyLinkedList = SinglyLinkedList()

        inputList = [1, 2, 1, 2]
        singlyLinkedList.populate(inputList)

        s = Solution(singlyLinkedList)

        expectedValue = False  # Considering empty list is a palindrome
        receivedValue = s.method1()
        self.assertTrue(
            expectedValue == receivedValue,
            "Expected: %s, Received: %s" % (expectedValue, receivedValue))
    def test_reverse_a_list_with_many_elements(self):
        originalList = SinglyLinkedList()
        inputList = range(10,101,10)
        originalList.populate(inputList)

        s = Solution()

        s.reverseOriginalList_Iterative(originalList)

        expectedList = inputList[::-1]
        returnedList = originalList.returnLinkedListAsList()

        self.assertTrue(expectedList == returnedList, "Expected: %s, Got: %s" %(expectedList, returnedList))
示例#25
0
    def test_changing_list_length_during_insert_at_head_operation(self):
        singlyLinkedListObject = SinglyLinkedList()

        expectedLength = 0
        actualLength = len(singlyLinkedListObject)
        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))

        inputList = range(10)
        for data in inputList:
            singlyLinkedListObject.insertAtHead(data)

            actualLength = len(singlyLinkedListObject)
            expectedLength += 1  # Because a new element has been added to the list.
            self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))
    def test_partition_number_present_in_middle_of_list(self):

        inputList = [38, 76, 72, 44, 17, 65, 24, 59, 47, 76]

        givenSinglyLinkedList = SinglyLinkedList()
        givenSinglyLinkedList.populate(inputList)

        s = Solution(givenSinglyLinkedList)

        returnList = s.partitionLinkedListBasedOnAGivenNumber_createNewList(38)
        outputList = [17, 24, 38, 76, 72, 44, 65, 59, 47, 76]

        returnedList = returnList.returnLinkedListAsList()
        self.assertTrue(returnedList == outputList,
                        "Expected: %s, Got: %s" % (outputList, returnedList))
示例#27
0
    def test_length_of_empty_list(self):
        singlyLinkedListObject = SinglyLinkedList()

        expectedLength = 0
        actualLength = len(singlyLinkedListObject)

        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))
示例#28
0
    def test_add_without_carry_basic_case_second_list_empty_first_list_has_more_than_one_element(
            self):
        firstList = SinglyLinkedList()
        inputValue = range(10)
        firstList.populate(inputValue)

        secondList = SinglyLinkedList()

        s = Solution(firstList, secondList)

        returnLinkedList = s.addTwoReversedIntegersAsLinkedListsAndCreateNewList(
        )
        outputValue = returnLinkedList.returnLinkedListAsList()
        expectedValue = inputValue
        self.assertTrue(outputValue == expectedValue,
                        "Expected: %s, Got: %s" % (expectedValue, outputValue))
示例#29
0
    def test_changing_list_length_during_delete_head_operation(self):
        singlyLinkedListObject = SinglyLinkedList()

        inputList = range(10)
        singlyLinkedListObject.populate(inputList)

        expectedLength = len(inputList)
        actualLength = len(singlyLinkedListObject)
        self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))

        for data in inputList:
            singlyLinkedListObject.deleteHead()

            actualLength = len(singlyLinkedListObject)
            expectedLength -= 1  # Because a new element has been added to the list.
            self.assertTrue(expectedLength == actualLength, "Expected length: %s, Actual Length: %s" %(expectedLength, actualLength))
示例#30
0
    def test_delete_head_with_one_element_list(self):
        singlyLinkedListObject = SinglyLinkedList()
        singlyLinkedListObject.populate([1])

        singlyLinkedListObject.deleteHead()
        outputList = singlyLinkedListObject.returnLinkedListAsList()
        self.assertTrue(len(outputList) == 0, "After deleting the only element in the list, Output List must be empty")
    def test_empty_list(self):
        singlyLinkedList = SinglyLinkedList()

        s = Solution(singlyLinkedList)

        expectedValue = True  # Considering empty list is a palindrome
        receivedValue = s.method1()
        self.assertTrue(
            expectedValue == receivedValue,
            "Expected: %s, Received: %s" % (expectedValue, receivedValue))
    def partitionLinkedListBasedOnAGivenNumber_createNewList(
            self, givenNumber):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()

        for data in self.givenSinglyLinkedList:
            if data < givenNumber:
                firstList.appendToListUsingTail(data)
            else:
                secondList.appendToListUsingTail(data)

        # print firstList.returnLinkedListAsList()
        # print secondList.returnLinkedListAsList()

        if firstList.isListEmpty():
            return secondList
        elif secondList.isListEmpty():
            return firstList
        else:
            firstList.tail.nextPointer = secondList.head  # Could be the other way round as well (secondList.tail.nextPointer = firstList.head ), but since it wasn't asked in the question, I've chosen the first option. In a real interview, I would ask the interviewer
            return firstList
    def test_add_with_carry_forever_carry_till_last(self):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()
        outputList = SinglyLinkedList()

        firstList.populate([9,9,9,9,9,9,9])
        secondList.populate([1])

        s = Solution(firstList, secondList, outputList)
        s.addTwoNonReversedIntegersAsLinkedListsAndCreateNewList()

        outputValue = outputList.returnLinkedListAsList()
        expectedValue = [1, 0, 0, 0, 0, 0, 0, 0]
        self.assertTrue(outputValue == expectedValue, "Expected: %s, Got: %s" %(expectedValue, outputValue))
    def test_add_without_left_over_carry_both_lists_have_unequal_items(self):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()
        outputList = SinglyLinkedList()

        firstList.populate([7,1,6,9])
        secondList.populate([5,9,2])

        s = Solution(firstList, secondList, outputList)
        s.addTwoNonReversedIntegersAsLinkedListsAndCreateNewList()

        outputValue = outputList.returnLinkedListAsList()
        expectedValue = [7,7,6,1]
        self.assertTrue(outputValue == expectedValue, "Expected: %s, Got: %s" %(expectedValue, outputValue))
    def partitionLinkedListBasedOnAGivenNumber_DontCreateNewList(
            self, givenNumber):
        # check head against given number. Move to appropriate list. advance head to next. delete previous head

        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()

        for data in self.givenSinglyLinkedList:
            if data < givenNumber:
                firstList.appendToListUsingTail(data)
            else:
                secondList.appendToListUsingTail(data)
            self.givenSinglyLinkedList.deleteHead(
            )  # Deleting head as the data is already under the relevant - firstList or secondList

        if firstList.isListEmpty():
            return secondList
        elif secondList.isListEmpty():
            return firstList
        else:
            firstList.tail.nextPointer = secondList.head  # Could be the other way round as well (secondList.tail.nextPointer = firstList.head ), but since it wasn't asked in the question, I've chosen the first option. In a real interview, I would ask the interviewer
            return firstList
示例#36
0
    def test_add_with_carry_forever_carry_till_last(self):
        firstList = SinglyLinkedList()
        firstList.populate([9, 9, 9, 9, 9, 9, 9])

        secondList = SinglyLinkedList()
        secondList.populate([1])

        s = Solution(firstList, secondList)

        returnLinkedList = s.addTwoReversedIntegersAsLinkedListsAndCreateNewList(
        )
        outputValue = returnLinkedList.returnLinkedListAsList()
        expectedValue = [0, 0, 0, 0, 0, 0, 0, 1]
        self.assertTrue(outputValue == expectedValue,
                        "Expected: %s, Got: %s" % (expectedValue, outputValue))
    def test_without_carry_best_case_equal_elements_three_in_length(self):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()
        outputList = SinglyLinkedList()

        firstList.populate([1, 1, 8])
        secondList.populate([4, 3, 1])

        s = Solution(firstList, secondList, outputList)
        s.addTwoNonReversedIntegersAsLinkedListsAndCreateNewList()

        outputValue = outputList.returnLinkedListAsList()

        expectedValue = [5, 4, 9]
        self.assertTrue(outputValue == expectedValue, "Expected: %s, Got: %s" %(expectedValue, outputValue))
示例#38
0
    def test_add_without_carry_both_lists_have_equal_items(self):
        firstList = SinglyLinkedList()
        firstList.populate([7, 1, 6])

        secondList = SinglyLinkedList()
        secondList.populate([5, 9, 2])

        s = Solution(firstList, secondList)

        returnLinkedList = s.addTwoReversedIntegersAsLinkedListsAndCreateNewList(
        )
        outputValue = returnLinkedList.returnLinkedListAsList()
        expectedValue = [2, 1, 9]
        self.assertTrue(outputValue == expectedValue,
                        "Expected: %s, Got: %s" % (expectedValue, outputValue))
    def test_reverse_a_list_with_many_elements(self):
        givenList = SinglyLinkedList()
        inputList = range(10,101,10)
        givenList.populate(inputList)

        s = Solution()

        outputLinkedList = SinglyLinkedList()
        s.returnReversedListUsingRecursion(givenList.head, outputLinkedList)
        
        expectedList = inputList[::-1]
        returnedList = outputLinkedList.returnLinkedListAsList()
        self.assertTrue(expectedList == returnedList, "Expected: %s, Got: %s" %(expectedList, returnedList))
    def partitionLinkedListBasedOnAGivenNumber_createNewList(self, givenNumber):
        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()

        for data in self.givenSinglyLinkedList:
            if data < givenNumber:
                firstList.appendToListUsingTail(data)
            else:
                secondList.appendToListUsingTail(data)

        # print firstList.returnLinkedListAsList()
        # print secondList.returnLinkedListAsList()

        if firstList.isListEmpty():
            return secondList
        elif secondList.isListEmpty():
            return firstList
        else:
            firstList.tail.nextPointer = secondList.head # Could be the other way round as well (secondList.tail.nextPointer = firstList.head ), but since it wasn't asked in the question, I've chosen the first option. In a real interview, I would ask the interviewer
            return firstList
示例#41
0
    def test_delete_head_from_a_list_with_more_than_two_elements(self):
        singlyLinkedListObject = SinglyLinkedList()
        inputList = range(20)
        singlyLinkedListObject.populate(inputList)

        # Deleting the first element from the inputList. this will help in assertion
        inputList.pop(0)

        singlyLinkedListObject.deleteHead()
        outputList = singlyLinkedListObject.returnLinkedListAsList()
        self.assertTrue(inputList == outputList, "Expecting one lesser value in the Output List but got different. Expected: %s, Got: %s" %(inputList, outputList))

        self.assertTrue(singlyLinkedListObject.head != singlyLinkedListObject.tail, "Tail and Head must NOT be the same after only 1 element remains in the list")
示例#42
0
    def test_append_when_list_has_at_least_one_element(self):
        singlyLinkedListObject = SinglyLinkedList()
        inputList = range(10)
        singlyLinkedListObject.populate(inputList)

        inputVal = 10
        singlyLinkedListObject.appendToListUsingTail(inputVal)

        self.assertTrue(singlyLinkedListObject.tail.data == inputVal, "Expected Tail data: %s, Got: %s" %(inputVal, singlyLinkedListObject.tail.data))

        outputList = singlyLinkedListObject.returnLinkedListAsList()
        outputVal = outputList[-1]
        self.assertTrue(inputVal == outputVal, "Expected tail value: %s, Got: %s" %(inputVal, outputVal))
    def partitionLinkedListBasedOnAGivenNumber_DontCreateNewList(self, givenNumber):
        # check head against given number. Move to appropriate list. advance head to next. delete previous head


        firstList = SinglyLinkedList()
        secondList = SinglyLinkedList()

        for data in self.givenSinglyLinkedList:
            if data < givenNumber:
                firstList.appendToListUsingTail(data)
            else:
                secondList.appendToListUsingTail(data)
            self.givenSinglyLinkedList.deleteHead() # Deleting head as the data is already under the relevant - firstList or secondList

        if firstList.isListEmpty():
            return secondList
        elif secondList.isListEmpty():
            return firstList
        else:
            firstList.tail.nextPointer = secondList.head # Could be the other way round as well (secondList.tail.nextPointer = firstList.head ), but since it wasn't asked in the question, I've chosen the first option. In a real interview, I would ask the interviewer
            return firstList