示例#1
0
 def test_remove(self):
     list = MyList(1, 2, 3, 4)
     self.assertEqual(list.remove(3), 4)
     self.assertIs(list.remove(6), None)
     self.assertEqual(list.size(), 3)
     with self.assertRaises(TypeError):
         list.remove('not an int')
示例#2
0
from my_list import MyList
from list_iterator import ListIterator
from non_zero_list_iterator import NonZeroListIterator

if __name__ == "__main__":
    data = [1, 2, 0, 0, 5, 6, 1, 0, 9, 8, 3, 0]
    ml = MyList(*data)

    print("Iterator passing through whole list:")
    i = ListIterator(ml)
    i.first()
    while not i.is_done():
        print(i.current_item(), end=', ')
        i.next()

    print("\n\nIterator passing through only non zero elements of list:")
    nzi = NonZeroListIterator(ml)
    nzi.first()
    while not nzi.is_done():
        try:
            print(nzi.current_item(), end=', ')
            nzi.next()
        except:
            pass  # last elem is 0
示例#3
0
 def init(self):
     return MyList()
示例#4
0
from my_list import MyList

numbers = MyList()
numbers.append(3)
numbers.append(3)
numbers.append(5)
numbers.append(7)
numbers.append(8)
numbers.append(3)

even_nos = MyList()
even_nos.append(10)

numbers.remove_odd()

# [3, 3, 5, 7, 8, 3]
print(numbers.items)
# [10]
print(even_nos.items)
 def test_clear(self):
     list = MyList(1, 2, 3, 4, 5)
     list.clear()
     self.assertEqual(list.get_size(), 0)
 def test_get_size(self):
     list = MyList(1, 2, 3, 4, 5)
     self.assertEqual(list.get_size(), 5)
 def test_remove_item(self):
     list = MyList(1, 2, 3, 4, 5)
     self.assertEqual(list.remove(3), 4)
     self.assertEqual(list.get_size(), 4)
     self.assertIs(list.remove(20), None)
 def test_get_index(self):
     list = MyList(1, 2, 3, 4, 5)
     self.assertEqual(list.get_index_of(3), 2)
     self.assertEqual(list.get_index_of(20), -1)
 def test_get(self):
     list = MyList(1, 2, 3, 4, 5)
     self.assertEqual(list.get(2), 3)
     self.assertIs(list.get(20), None)
 def test_has_item(self):
     list = MyList(1, 2, 3, 4, 5)
     self.assertTrue(list.has(2), True)
     self.assertFalse(list.has(20), False)
 def test_add_items(self):
     list = MyList(1, 2, 3, 4, 5)
     list.add(6, 7, 8, 9, 10)
     self.assertEqual(list.get_size(), 10)
 def get_list(self):
     l = MyList()
     l.add(1)
     l.add(2)
     l.add(3)
     return l
示例#13
0
 def test_size(self):
     list = MyList(1, 2, 3, 4)
     self.assertEqual(list.size(), 4)
示例#14
0
 def test_get_at(self):
     list = MyList(1, 2, 3, 4)
     self.assertEqual(list.get_at(3), 4)
     self.assertIs(list.get_at(6), None)
     with self.assertRaises(TypeError):
         list.get_at('not an int')
示例#15
0
 def test_exists(self):
     list = MyList(1, 2, 3, 4)
     self.assertTrue(list.exists(2), True)
     self.assertFalse(list.exists(8), False)
示例#16
0
 def test_add(self):
     list = MyList(1, 2, 3, 4)
     list.add(5, 6, 7, 8)
     self.assertEqual(list.size(), 8)