コード例 #1
0
def addTwoLists(first, second):
    firstRunner = first.head
    secondRunner = second.head
    result = LinkedList()
    residue = 0
    while firstRunner.next or secondRunner.next or residue == 1:
        # process the first runner
        # it will keep going only if it has a next pointer
        if firstRunner.next:
            firstValue = firstRunner.next.value
            firstRunner = firstRunner.next
        else:
            firstValue = 0
        # process the second one
        if secondRunner.next:
            secondValue = secondRunner.next.value
            secondRunner = secondRunner.next
        else:
            secondValue = 0
        # calculate the sum
        sumV = firstValue + secondValue + residue
        # check whether it is too big for single digit
        if sumV > 9:
            sumV -= 10
            residue = 1
        else:
            residue = 0
        result.append(sumV)
    return result
コード例 #2
0
 def testLegitPos(self):
     testList = LinkedList()
     for i in range(100):
         testList.append(i)
     mapping = range(100)
     for i in range(99):
         # keep deleting the first element
         delNode(testList, 0)
         del mapping[0]
         for i, e in enumerate(testList):
             self.assertEqual(e, mapping[i])
コード例 #3
0
 def test(self):
     testList = LinkedList()
     # populate
     for i in range(5):
         testList.append(i)
     # 5th to 10th should be None, because 0-1-2-3-4, 5th is beyond the first element
     for i in range(10, 4, -1):
         self.assertEqual(lastKthElement(testList, i), None)
     # 0th to 4th should not be None
     for i in range(4, -1, -1):
         self.assertEqual(lastKthElement(testList, i).value, 4 - i)
     # negative index
     for i in range(-10, 0):
         self.assertEqual(lastKthElement(testList, i), None)
コード例 #4
0
def partition(input_list, partitioner):
    pre = LinkedList()
    post = LinkedList()
    for val in input_list:
        if val < partitioner:
            pre.append(val)
        else:
            post.append(val)
    # connecting pre and post lists
    pre.last.next = post.head.next
    return pre
コード例 #5
0
 def test(self):
     # empty
     a = LinkedList()
     self.assertTrue(isPalindrome(a))
     # one element
     a.append("C")
     self.assertTrue(isPalindrome(a))
     # two elements
     a.append("A")
     self.assertFalse(isPalindrome(a))
     # three elements
     a.append("C")
     self.assertTrue(isPalindrome(a))
コード例 #6
0
 def testIllegalPos(self):
     testList = LinkedList()
     # populate
     testList.append(0)
     self.assertRaises(IndexError, delNode, testList, -1)
     self.assertRaises(IndexError, delNode, testList, 0)
     testList.append(1)
     delNode(testList, 0)
     self.assertEqual(testList.get(0).value, 1)
     self.assertRaises(IndexError, delNode, testList, 0)
コード例 #7
0
 def testNoPartition(self):
     testList = LinkedList()
     for i in range(10):
         testList.append(i)
     res = partition(testList, 0)
     res2 = partition(testList, 10)
     for i, v in enumerate(testList):
         self.assertEqual(v, res.get(i).value)
         self.assertEqual(v, res2.get(i).value)
     # second method
     partitionInPlace(testList, 0)
     mapping = range(10)
     for v in testList:
         # make sure every element in the partitioned
         # list has an equivalent in mapping
         self.assertTrue(v in mapping)
         mapping.remove(v)
     mapping = range(10)
     partitionInPlace(testList, 10)
     for v in testList:
         self.assertTrue(v in mapping)
         mapping.remove(v)
コード例 #8
0
 def testNormalLists(self):
     # both methods should return None
     a = LinkedList()
     for method in self.methods:
         self.assertEqual(method(a), None)
     # non empty
     a.append(9)
     a.append(6)
     a.append(3)
     for method in self.methods:
         self.assertEqual(method(a), None)
コード例 #9
0
 def testAddEmpty(self):
     # two empty lists
     empty1 = LinkedList()
     empty2 = LinkedList()
     empty3 = addTwoLists(empty1, empty2)
     self.assertEqual(empty3.head.next, None)
     # only one empty list
     nonEmpty = LinkedList()
     nonEmpty.append(1)
     res = addTwoLists(empty1, nonEmpty)
     self.assertEqual(res.head.next.value, 1)
     self.assertEqual(res.head.next.next, None)
     res = addTwoLists(nonEmpty, empty1)
     self.assertEqual(res.head.next.value, 1)
     self.assertEqual(res.head.next.next, None)
コード例 #10
0
 def testReversing(self):
     # empty
     empty = LinkedList()
     res = reverseList(empty)
     self.assertEqual(res.head.next, None)
     # one element
     empty.append(99)
     res = reverseList(empty)
     self.assertEqual(res.head.next.value, 99)
     self.assertEqual(res.head.next.next, None)
     # multiple elements
     empty.append(1)
     res = reverseList(empty)
     content = [1, 99]
     self.assertEqual([v for v in res], content)
コード例 #11
0
from List import LinkedList
from Stack import Stack
from Queue import Queue
from BSTree import BSTree
from AVLTree import AVLTree

linked_list = LinkedList()
stack = Stack()
queue = Queue()
bstree = BSTree()
avltree = AVLTree()


class Manager:
    @classmethod
    def add_item(cls, num, struck_type):

        struckture = cls.get_struck_obj(struck_type)

        if not (num in struckture):

            if struckture.add(num):
                return "El numero se agrego de manera exitosa\n{}".format(
                    struckture)

            else:
                return "*** Se produjo un error al agregar ***"
        else:
            if struckture == bstree or avltree:
                return "*** El numero ya existe en el " + struck_type + " ***"
            else:
コード例 #12
0
from List import LinkedList

listt = LinkedList()

#Insert elements
listt.append(1)
listt.append(3)
listt.append("a")
listt.append(True)

#Print list
print(listt)

#Print list size
print(listt.__len__())

#Get node by index
print(listt.__getitem__(3))

#Set item by index
listt.__setitem__(3, "watermelon")

#Print list again
print(listt)

#Get fist index by value
print(listt.index("watermelon"))

#Insert element with index specified
listt.insert(2, "apple")
コード例 #13
0
 def testEmptyList(self):
     empty = LinkedList()
     res = partition(empty, 0)
     self.assertRaises(IndexError, res.get, 0)
     partitionInPlace(empty, 1000)
     self.assertRaises(IndexError, empty.get, 0)
コード例 #14
0
 def testCorruptedLists(self):
     # A->B->C->D->E->C
     a = LinkedList()
     a.append('A')
     a.append('B')
     a.append('C')
     intertwined = a.last
     a.append('D')
     a.append('E')
     a.last.next = intertwined
     for method in self.methods:
         self.assertEqual(method(a), intertwined)
     # A->A
     b = LinkedList()
     b.append('A')
     intertwined = b.last
     b.last.next = intertwined
     for method in self.methods:
         self.assertEqual(method(b), intertwined)
コード例 #15
0
    def testAddHard(self):
        contentA = [5, 6, 7, 1]
        contentB = [5, 3, 2]
        listA = LinkedList()
        listB = LinkedList()
        for v in contentA:
            listA.append(v)
        for v in contentB:
            listB.append(v)
        contentC = [0, 0, 0, 2]
        listC = addTwoLists(listA, listB)
        self.assertEqual([v for v in listC], contentC)

        # second case
        contentA = [5]
        contentB = [6]
        listA = LinkedList()
        listB = LinkedList()
        for v in contentA:
            listA.append(v)
        for v in contentB:
            listB.append(v)
        contentC = [1, 1]
        listC = addTwoLists(listA, listB)
        self.assertEqual([v for v in listC], contentC)
コード例 #16
0
 def testAddSimple(self):
     listA = LinkedList()
     listB = LinkedList()
     listA.append(1)
     listA.append(2)
     listA.append(3)
     listB.append(4)
     listB.append(5)
     listB.append(6)
     res = addTwoLists(listA, listB)
     content = [v for v in res]
     self.assertEqual(content, [5, 7, 9])
コード例 #17
0
    def testPartition(self):
        testList = LinkedList()
        data = [1, 45, 28, 20, 60, 24, 24, 22, 2]
        for i in data:
            testList.append(i)
        res = partition(testList, 24)
        res_list = [v for v in res]
        self.assertEqual(res_list, [1, 20, 22, 2, 45, 28, 60, 24, 24])

        testList = LinkedList()
        data = [1, 1, 1, 1, 2]
        for i in data:
            testList.append(i)
        res = partition(testList, 1)
        res_list = [v for v in res]
        self.assertEqual(res_list, [1, 1, 1, 1, 2])

        # method 2
        testList = LinkedList()
        data = [-99, 100, 33, 44, -55, 78, 9, 8, 0]
        for d in data:
            testList.append(d)
        partitionInPlace(testList, 9)
        res = [v for v in testList]
        # [-99, -55, 0, 8]
        # [100, 33, 44, 78, 9]
        for i in range(4):
            self.assertTrue(res[i] < 9)
        for i in range(4, 9):
            self.assertTrue(res[i] >= 9)
        data.sort()
        res.sort()
        self.assertEqual(data, res)
コード例 #18
0
def reverseList(input_list):
    newList = LinkedList()
    content = reversed([v for v in input_list])
    for v in content:
        newList.append(v)
    return newList
コード例 #19
0
ファイル: test.py プロジェクト: pavelzamyatin/algorithms
from List import LinkedList

lst = LinkedList()
new_lst = LinkedList()

# ----- TESTING APPEND ----- #
lst.append('First')
lst.append('Second')
lst.append('Third')
new_lst.append('Only one element')

# ----- TESTING GET ----- #
lst.prepend('Now it is first')

print(lst)

# ----- TESTING GET ----- #
try:
	print(lst.get(2))
	print(lst.get(3))
except IndexError:
	print("IndexError: Out of Index")

# ----- TESTING LEN ----- #
print('Len method:', len(lst))
print('Length method:', lst.length())

# ----- TESTING POP ----- #
print('Pop method result:', lst.pop())
print(lst)
print('Len after pop:', len(lst))