Esempio n. 1
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))
Esempio n. 2
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))
Esempio n. 3
0
    def test_changing_list_length_during_append_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.appendToListUsingTail(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 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 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 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
    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
    def addTwoReversedIntegersAsLinkedListsAndCreateNewList(self):
        outputList = SinglyLinkedList()

        carry = 0
        firstListCurrentNode = self.firstList.head
        secondListCurrentNode = self.secondList.head
        while firstListCurrentNode is not None and secondListCurrentNode is not None:
            sum = carry + firstListCurrentNode.data + secondListCurrentNode.data
            data = sum % 10
            carry = sum / 10
            # print "data: %s" %data
            # print "carry: %s" %carry
            outputList.appendToListUsingTail(data)
            firstListCurrentNode = firstListCurrentNode.nextPointer
            secondListCurrentNode = secondListCurrentNode.nextPointer

        # there could be a left-over carry. Need to add it to the list now, but before that, need to check if the number of items in the list were uneven
        currentNode = None
        if firstListCurrentNode is None:
            currentNode = secondListCurrentNode
        else:
            currentNode = firstListCurrentNode
        # If both firstListCurrentNode and secondListCurrentNode are None, it is implicitly covered

        while currentNode is not None:
            sum = carry + currentNode.data
            data = sum % 10
            carry = sum / 10

            outputList.appendToListUsingTail(data)

            currentNode = currentNode.nextPointer

        # Now, check the left-over carry and add it to the list if it is non-zero
        if carry != 0:
            outputList.appendToListUsingTail(carry)

        # print outputList.returnLinkedListAsList()
        return outputList