Пример #1
0
 def test_searching(self):
     l = UnorderedList()
     for i in range(4):
         l.add(i)
     for i in range(4):
         self.assertTrue(l.search(i))
     for item in (5, None, True, "blah"):
         self.assertFalse(l.search(item))
Пример #2
0
 def test_adding(self):
     l = UnorderedList()
     self.assertEqual(l.size(), 0)
     self.assertTrue(l.is_empty())
     l.add(1)
     self.assertEqual(l.size(), 1)
     self.assertEqual(l.head.value, 1)
     self.assertFalse(l.is_empty())
     l.add(2)
     self.assertEqual(l.size(), 2)
     self.assertEqual(l.head.value, 2)
Пример #3
0
 def test_remove(self):
     l = UnorderedList()
     for i in range(3):
         l.add(i)
     # remove from middle
     l.remove(1)
     self.assertFalse(l.search(1))
     self.assertEqual(l.size(), 2)
     # remove from end
     l.remove(2)
     self.assertFalse(l.search(2))
     self.assertEqual(l.size(), 1)
     # remove from start
     l.remove(0)
     self.assertFalse(l.search(0))
     self.assertEqual(l.size(), 0)
Пример #4
0
from unordered_list import UnorderedList

mylist = UnorderedList()

mylist.add(20)
mylist.add(40)
mylist.insert(1, 55)
mylist.insert(-2, 65)
print(mylist.index(65))
mylist.append(100)
print(mylist.index(100))
Пример #5
0
from unordered_list import UnorderedList


def remove_dups(linked_list):
    start = linked_list.head
    previous = None

    while start:
        my_next = start.get_next()
        previous = start

        while my_next:
            if start.get_data() == my_next.get_data():
                previous.set_next(my_next.get_next())
            previous = my_next
            my_next = my_next.get_next()  #advance to next node

        start = start.get_next()


my_list = UnorderedList()
stuff = [4, 2, 44, 23, 2, 3, 4, 9]

for i in stuff:
    my_list.add_item(i)

print(my_list)
remove_dups(my_list)
print(my_list)
 def setUp(self):
     self.l = UnorderedList()
class TestUnorderedList(unittest.TestCase):
    def setUp(self):
        self.l = UnorderedList()

    def test_all(self):
        self.assertTrue(self.l.isEmpty())
        # 54 26 93 17 77 31
        self.l.add(31)
        self.l.add(77)
        self.l.add(17)
        self.l.add(93)
        self.l.add(26)
        self.l.add(54)

        self.assertFalse(self.l.isEmpty())
        self.assertEqual(6, self.l.size())
        self.assertTrue(self.l.search(93))
        self.assertEqual(2, self.l.index(93))
        self.assertFalse(self.l.search(100))
        self.assertIsNone(self.l.index(100))

        # 100 54 26 93 17 77 31
        self.l.add(100)
        self.assertTrue(self.l.search(100))
        self.assertEqual(0, self.l.index(100))
        self.assertEqual(7, self.l.size())

        # 100 26 93 17 77 31
        self.assertEqual(2, self.l.index(26))
        self.l.remove(54)
        self.assertEqual(6, self.l.size())
        self.assertEqual(1, self.l.index(26))
        # 100 26 17 77 31
        self.assertEqual(3, self.l.index(17))
        self.l.remove(93)
        self.assertEqual(5, self.l.size())
        self.assertEqual(2, self.l.index(17))
        # 100 26 17 77
        self.assertEqual(3, self.l.index(77))
        self.l.remove(31)
        self.assertEqual(4, self.l.size())
        self.assertEqual(3, self.l.index(77))
        self.assertFalse(self.l.search(93))

    def test_appendInsertPop(self):
        # 31 93 77 17
        self.l.append(77)
        self.l.insert(31, 0)
        self.l.append(17)
        self.l.insert(93, 1)

        self.assertEqual(4, self.l.size())
        self.assertEqual(0, self.l.index(31))
        self.assertEqual(1, self.l.index(93))
        self.assertEqual(2, self.l.index(77))
        self.assertEqual(3, self.l.index(17))

        # 31 93 77
        self.l.pop()
        self.assertEqual(0, self.l.index(31))
        self.assertEqual(1, self.l.index(93))
        self.assertEqual(2, self.l.index(77))

        # 31 77
        self.l.pop(1)
        self.assertEqual(0, self.l.index(31))
        self.assertEqual(1, self.l.index(77))

    def test_dupitem(self):
        self.l.append(3)
        self.l.append(3)
        self.l.append(2)
        self.l.append(3)

        self.assertEqual(4, self.l.size())
        self.l.remove(3)
        self.assertTrue(self.l.search(3))
        self.assertEqual(0, self.l.index(3))
        self.assertEqual('[3, 2, 3]', self.l.__str__())

    def test_slice(self):
        self.assertEqual([], self.l.slice(0, 5))
        # 1 2 3 4 5
        self.l.append(1)
        self.l.append(2)
        self.l.append(3)
        self.l.append(4)
        self.l.append(5)
        self.assertEqual([1, 2], self.l.slice(0, 2))
        self.assertEqual([3, 4, 5], self.l.slice(2, 5))