示例#1
0
def partition(list, x):

    if (list.head == None):
        return

    leftList = List()
    rightList = List()

    current = list.head
    while True:

        if (current.data < x):
            leftList.insert(current.data)
        else:
            rightList.insert(current.data)

        current = current.next

        if (current == None):
            break

    lastLeftListNode = leftList.head
    while (lastLeftListNode.next != None):
        lastLeftListNode = lastLeftListNode.next

    lastLeftListNode.next = rightList.head
    list.head = leftList.head
示例#2
0
    def test_listInsert(self):
        me = List(values=(1, 2, 3, 4))
        me.insert(Element(10), 2)
        assert me.get_position(2).value == 10, \
            "should have inserted element with value 10 at position 2"
        assert me.get_position(1).value == 2, \
            "should still have element with value 2 at position 1"
        assert me.get_position(3).value == 3, \
            "should still have element with value 3 at position 3"
        me.insert(Element(42), 5)
        assert me.get_position(5).value == 42, \
            "should have inserted element at the end"
        with pytest.raises(ValueError, \
            match="Insert value higher than list length"):
            me.insert(Element(99), 99)
        with pytest.raises(ValueError, \
            match="Insert value must at least be zero"):
            me.insert(Element(-1), -1)

        me.insert(Element(0), 0)
        assert me.get_position(0).value == 0, \
            "should have inserted element at the beginning"
        me = List()
        me.insert(Element(-1), 0)
        assert me.get_position(0).value == -1, \
            "should have inserted initial element at the beginning"
示例#3
0
    def testAdd(self):

        list1 = List()
        list2 = List()

        list1.insert(6)
        list1.insert(1)
        list1.insert(7)

        list2.insert(2)
        list2.insert(9)
        list2.insert(5)

        result = add(list1, list2)
        self.assertEqual(result.toList(), [2, 1, 9])

        list1 = List()
        list2 = List()

        list1.insert(3)

        list2.insert(7)
        list2.insert(7)

        result = add(list1, list2)
        self.assertEqual(result.toList(), [0, 8])
示例#4
0
def add(list1, list2):

    result = List()
    a = list1.head
    b = list2.head
    carry = 0

    while (a != None and b != None):
        value = a.data + b.data + carry
        result.append(value % 10)
        carry = value / 10
        a = a.next
        b = b.next

    while (a != None):
        value = a.data + carry
        result.append(value % 10)
        carry = value / 10
        a = a.next

    while (b != None):
        value = b.data + carry
        result.append(value % 10)
        carry = value / 10
        b = b.next

    if (carry != 0):
        result.append(carry)

    return result
示例#5
0
    def test_delete(self):
        me = List(values=(1, 2, 3, 4))
        me.delete(2)
        assert me.get_position(1).value == 3, \
            "should show next element on position of deleted element"
        me.delete(1)
        assert me.get_position(0).value == 3, \
            "should delete first element"
        me.delete(4)
        assert me.get_position(0).value == 3, \
            "should delete last element"

        with pytest.raises(ValueError, \
            match="Element 9 not in List"):
            me.delete(9)

        me.delete(3)
        with pytest.raises(ValueError, \
            match="No elements in List"):
            me.delete(0)

        assert me.get_element(2) == (None, -1), \
            "should not find element with value 2 anymore"


# if __name__ == '__main__':
#     testListSetup()
#     testGetPosition()
#     testListAppend()
#     testListInsert()
#     testDelete()
#     print("Yay, everything has passed! 😃")
示例#6
0
    def testPartition(self):

        list = List()
        list.insert(9)
        list.insert(7)
        list.insert(50)
        list.insert(6)
        list.insert(5)
        list.insert(2)
        list.insert(3)

        partition(list, 8)
        self.assertEqual(list.toList(), [7, 6, 5, 2, 3, 9, 50])

        list = List()
        list.insert(1)

        partition(list, 2)
        self.assertEqual(list.toList(), [1])
示例#7
0
 def test_listAppend(self):
     me = List()
     me.append(Element(6))
     assert me.get_position(0).value == 6, \
         "should have inserted Element with value 0 at position 0"
     me.append(Element(60))
     assert me.get_position(1).value == 60, \
         "should have inserted Element with value 0 at position 0"
     me.append(Element(16))
     assert me.get_position(2).value == 16, \
         "should have inserted Element with value 0 at position 0"
示例#8
0
    def testFindLoop(self):

        list = List()
        a = Node('A')
        b = Node('B')
        c = Node('C')
        d = Node('D')
        e = Node('E')

        list.head = a
        a.next = b
        b.next = c
        c.next = d
        d.next = e
        e.next = c

        self.assertEqual(findLoop(list), c)
示例#9
0
from LinkedList import List, Node

# ------------------------------------------------

mylist = List()
mylist.insertlast(1)
mylist.insertlast(2)
mylist.insertlast(3)
mylist.insertlast(4)
mylist.insertlast(5)
mylist.printlist()
示例#10
0
 def setUp(self):
     self.list = List()
示例#11
0
from LinkedList import Node, List

n1 = Node(12)
n2 = Node(55)
n1.next = n2

linked_list = List()
linked_list.add_in_tail(n1)
linked_list.add_in_tail(n2)
linked_list.add_in_tail(Node(128))
linked_list.print_all_nodes()

nf = linked_list.find(55)
if nf is not None:
    print(nf.value)
示例#12
0
    def testPartitionEmptyList(self):

        list = List()

        partition(list, 8)
        self.assertEqual(list.toList(), [])
示例#13
0
 def test_getPosition(self):
     me = List(values=(1, 2, 3, 4))
     assert me.get_position(1).value == 2, \
         "should return element with value 2 at position 1"