Example #1
0
 def test_list_no_empty_add_value(self):
     """
     Test if a linked list is not empty after adding a node
     """
     #list_init = str(self.liste)
     self.liste.add_first(linkedList.Node("v"))
     #result= str(self.liste)
     self.assertFalse(self.liste.list_is_empty())
Example #2
0
 def test_first_element(self):
     """
     Test if the first element is the one added at the beginning
     of our linked list
     """
     self.liste.add_first(linkedList.Node("e"))
     result = str(self.liste)
     self.assertEqual(result[2], "e")
Example #3
0
 def test_no_modif(self):
     """
     Test if the linked list is not modified when simultaneously adding
     and removing the same element
     """
     list_init = str(self.liste)
     self.liste.add_first(linkedList.Node("v"))
     self.liste.remove_node("v")
     result = str(self.liste)
     self.assertEqual(result, list_init)
def partition(llist_head, x):

    # references for nodes with values
    # less than and greater than x
    less_head, less_tail = None, None
    greater_head, greater_tail = None, None

    # traverse linked list
    curr = llist_head
    next_node = None

    while curr:
        next_node = curr.next

        curr_copy = linkedList.Node(curr.data)
        # if curr node's value is less than x
        # insert it into less_nodes
        # and delete from input list
        if curr.data < x:
            if less_head is None:
                less_head = curr_copy
                less_tail = curr_copy
            else:
                curr_copy.next = less_head
                less_head = curr_copy
        # if curr node's value is equal to or
        # greater than x insert it into
        # greater_nodes and delete from
        # input list
        else:
            if greater_head is None:
                greater_head = curr_copy
                greater_tail = curr_copy
            else:
                curr_copy.next = greater_head
                greater_head = curr_copy

        curr = next_node

    # connect less than nodes to greater than nodes
    less_tail.next = greater_head

    return less_head
Example #5
0
def sumLists(num1, num2):

    multiplier = 1

    num1_curr = num1
    num2_curr = num2
    ans = 0

    # iterate through both lists,
    # stopping when both pointers
    # are null
    while num1_curr is not None or num2_curr is not None:

        # add product of multiplier
        # and each num to ans
        if num1_curr:
            product1 = multiplier * num1_curr.data
            ans += product1
            num1_curr = num1_curr.next
        if num2_curr:
            product2 = multiplier * num2_curr.data
            ans += product2
            num2_curr = num2_curr.next
        # multiply multiplier by ten with
        # every iteration
        multiplier *= 10

    digit = None
    output = None

    multiplier /= 10
    while ans != 0:
        digit = ans / multiplier
        node = linkedList.Node(digit)
        if output is None:
            output = node
        else:
            node.next = output
            output = node
        ans %= multiplier
        multiplier /= 10

    return output
Example #6
0
def translateTurns(turns):
    size = len(turns)
    linkList = linkedList.linkedList()
    i = 0
    firstState = np.copy(orient1)
    string = "x"
    rotation = None
    while i < size:
        if i+1 < size:
            if turns[i+1:i+2] == "'":
                rotation = turns[i:i+2]
                i += 3
            elif turns[i+1:+2] == "2":
                rotation = turns[i:i+2]
                i += 3
            else:
                rotation = turns[i:i+1]
                i += 2
        else:
            rotation = turns[i:i+1]
            i += 2
        newNode = linkedList.Node(rotation,firstState)
        string += rotation
        linkList.append(newNode)
    iterator = linkedList.iterator(linkList)
    while 1:
        turn = iterator.getTurn()
        #First state may be uninitialized, must figure out
        state = iterator.getState()
        #print(state[0])
        newTurn = translateMove(turn,state)
        iterator.updateTurn(newTurn)
        updateOrient(state,turn)
        if iterator.next():
            iterator.updateState(state)
        else:
            break
    return linkList
Example #7
0
import linkedList


def kthMember(lList, k):
    temp = []
    n = lList.head

    while (n != None):
        temp.append(n.data)
        n = n.next

    return temp[-k]


myHeadNode = linkedList.Node(1)
myHeadNode.appendToTail(2)
myHeadNode.appendToTail(1)
myHeadNode.appendToTail(3)
myHeadNode.appendToTail(2)
myHeadNode.appendToTail(4)
myHeadNode.appendToTail(3)

myLinkedList = linkedList.LinkedList(myHeadNode)
print('--- 連結リスト L ---')
myLinkedList.printLinkedList()

print('Lの後ろから3番目の要素は' + str(kthMember(myLinkedList, 3)) + 'です。')
Example #8
0
 def test_list_no_empty_add_value(self):
     """
     Test if a linked list is not empty after adding a node
     """
     self.liste1.add_first(linkedList.Node("v"))
     self.assertFalse(self.liste1.list_is_empty())