Esempio n. 1
0
def test_join():
    """
	Case 1: Both lists empty
	Case 2: List A empty
	Case 3: List B empty
	Case 4: Both lists normal
	"""
    list_a = linked_list()
    list_b = linked_list()
    list_a.join(list_b)
    assert list_a.traverse(
    ) == [] and list_a.size == 0 and list_a.front == None and list_a.back == None
    list_c = linked_list()
    list_c.add(1)
    list_c.add(2)
    list_c.add(3)
    list_a.join(list_c)
    assert list_a.traverse() == [
        1, 2, 3
    ] and list_a.size == 3 and list_a.front.data == 1 and list_a.back.data == 3
    list_d = linked_list()
    list_a.join(list_d)
    assert list_a.traverse() == [
        1, 2, 3
    ] and list_a.size == 3 and list_a.front.data == 1 and list_a.back.data == 3
    list_e = linked_list()
    list_e.add(4)
    list_e.add(5)
    list_e.add(6)
    list_a.join(list_e)
    assert list_a.traverse() == [
        1, 2, 3, 4, 5, 6
    ] and list_a.size == 6 and list_a.front.data == 1 and list_a.back.data == 6
Esempio n. 2
0
 def test_main(self):
     for (test, result) in self.test_case:
         ll = linked_list()
         ll.insert_multiple(list(test))
         value = remove_dups_from_LL(ll)
         expected_result = linked_list().insert_multiple(list(result))
         print(str(test), str(value))
         self.assertTrue(str(value), str(expected_result))
Esempio n. 3
0
    def setUp(self):
        self.empty = linked_list()
        self.one_item = linked_list([2])

        self.list_one = linked_list([1, 2, 3])
        self.list_two = linked_list([1, 2, 3])
        self.list_three = linked_list([1, 2, 4])
        self.list_four = linked_list([1, 2])
        self.list_five = linked_list([1, 2, 3, 4])
        self.list_six = linked_list([2, 2, 3])
        self.list_seven = linked_list([1, 4, 3])
Esempio n. 4
0
 def testRemove3(self):
     lst = linked_list()
     lst.insert(1)
     lst.insert(2)
     lst.insert(3)
     lst.remove(lst.head)
     self.assertEqual(tuple([2, 1]), lst.print_list())
Esempio n. 5
0
def create_linked_list(array):

    alist = linked_list()
    for element in array:
        alist.push_back(element)

    return alist
Esempio n. 6
0
 def testRemove2(self):
     lst = linked_list()
     lst.insert(1)
     lst.insert(2)
     short_list = lst.remove(lst.head)
     x = lst.print_list()
     self.assertEqual(x, tuple([1]))
Esempio n. 7
0
def test_get():
    """
	Case 1: List empty
	Case 2: Index 0
	Case 3: Middle index
	Case 4: Last index 
	Case 5: Out of bounds index
	"""
    list_ = linked_list()
    caught = False
    try:
        list_.get(1)
    except ValueError:
        caught = True
    assert caught
    caught = False
    list_.add(2)
    list_.add(3)
    list_.add(4)
    assert list_.get(0) == 2
    assert list_.get(1) == 3
    assert list_.get(2) == 4
    try:
        list_.get(3)
    except ValueError:
        caught = True
    assert caught
Esempio n. 8
0
def reverse_sum(llist1, llist2):
    ans = linked_list()
    temp_buf = 0
    curr1 = llist1.head
    curr2 = llist2.head
    while curr1.next is not None or curr2.next is not None:
        if curr1.next is not None:
            data1 = curr1.next.data
            curr1 = curr1.next
        else:
            data1 = 0

        if curr2.next is not None:
            data2 = curr2.next.data
            curr2 = curr2.next
        else:
            data2 = 0

        data = data1 + data2

        ans.append(data % 10 + temp_buf)
        temp_buf = data // 10

    if temp_buf > 0:
        ans.append(temp_buf)
    return ans
Esempio n. 9
0
 def testInsert(self):
     """insert should give a known result with known input"""
     item = 5
     l1 = linked_list()
     l1.insert(item)
     x = l1.print_list()
     y = tuple([5])
     self.assertEqual(x, y)
 def test_build_min_heap(self):
     L1 = linked_list(1)
     L2 = linked_list(2)
     L2.insert(linked_list_node(2))
     L2.insert(linked_list_node(2))
     L3 = linked_list(3)
     L3.insert(linked_list_node(3))
     L3.insert(linked_list_node(3))
     L4 = linked_list(4)
     L4.insert(linked_list_node(4))
     L5 = linked_list(5)
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     h = min_heap([L3, L4, L5, L2, L1])
     h.build_min_heap()
     self.assertEqual(h, [L1, L2, L5, L3, L4])
Esempio n. 11
0
def run():
    base_list = ['m', 'q', 'g', 'z', 'o', 'a', 'u', 'e', 'a', 'm', 's']
    link_list = linked_list()
    link_list.build(base_list)
    print(link_list)

    link_list = delete_middle(link_list)
    print(link_list)
def test_to_list_one():
    expected = "  3 2 1"
    my_list = linked_list()
    my_list.insert(1)
    my_list.insert(2)
    my_list.insert(3)
    actual = my_list.to_list()
    assert actual == expected
def test_to_string_one():
    expected = '  3 2 1'
    my_list = linked_list()
    my_list.insert(1)
    my_list.insert(2)
    my_list.insert(3)
    actual = my_list.to_string()
    assert actual == expected
Esempio n. 14
0
 def testRemove(self):
     lst = linked_list()
     lst.insert(1)
     lst.insert(2)
     lst.insert(3)
     lst.insert(4)
     lst.remove(lst.head.next)
     self.assertFalse(0, lst.head.data)
 def test_min_heapify(self):
     L1 = linked_list(1)
     L2 = linked_list(2)
     L2.insert(linked_list_node(2))
     L2.insert(linked_list_node(2))
     L3 = linked_list(3)
     L3.insert(linked_list_node(3))
     L3.insert(linked_list_node(3))
     L4 = linked_list(4)
     L4.insert(linked_list_node(4))
     L5 = linked_list(5)
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     h = min_heap([L5, L1, L2, L3, L4])
     h.min_heapify(0)
     self.assertEquals(h, [L1, L3, L2, L5, L4])
 def test_min_heapify(self):
     L1 = linked_list(1)
     L2 = linked_list(2)
     L2.insert(linked_list_node(2))
     L2.insert(linked_list_node(2))
     L3 = linked_list(3)
     L3.insert(linked_list_node(3))
     L3.insert(linked_list_node(3))
     L4 = linked_list(4)
     L4.insert(linked_list_node(4))
     L5 = linked_list(5)
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     h = min_heap([L5, L1, L2, L3, L4])
     h.min_heapify(0)
     self.assertEquals(h, [L1, L3, L2, L5, L4])
Esempio n. 17
0
def test_search():

    data, size, goal, r = parseLine(
        "8x8 \nw 1,2\nw 1,3\nw 1,4\nw 1,5\n robot 4,2\ngoal 7,0")
    world = create_world(size, data, r, goal)
    world.print_world()
    stack = linked_list()
    r2d2 = robot(r)
    search(r2d2.x, r2d2.y, world, stack)
def test_insert(build_empty_list, build_populated_list):
    empty_list = linked_list()
    populated_list, item1, item2, item3, item4, item5 = build_populated_list
    empty_list.insert('new')
    assert empty_list.head.next_node.value is None
    assert empty_list.head.value == 'new'
    populated_list.insert('new')
    assert populated_list.head.next_node.value == item1.value
    assert populated_list.head.value == 'new'
 def test_heap_minimum(self):
     L1 = linked_list(1)
     L1.insert(linked_list_node(1))
     L1.insert(linked_list_node(1))
     L2 = linked_list(2)
     L2.insert(linked_list_node(2))
     L2.insert(linked_list_node(2))
     L3 = linked_list(3)
     L3.insert(linked_list_node(3))
     L3.insert(linked_list_node(3))
     L4 = linked_list(4)
     L4.insert(linked_list_node(4))
     L5 = linked_list(5)
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     q = min_priority_queue([L1, L2, L3, L4, L5])
     self.assertEquals(q.heap_minimum().key, 1)
Esempio n. 20
0
def test_traverse():
    """
	Case 1: List empty
	Case 2: List non-empty
	"""
    list_ = linked_list()
    assert list_.traverse() == []
    list_.add(3)
    assert list_.traverse() == [3]
Esempio n. 21
0
 def testSearch(self):
     lst = linked_list()
     lst.insert(1)
     lst.insert(2)
     lst.insert(3)
     lst.insert(4)
     lst.insert(5)
     x = lst.search(7)
     self.assertFalse(0, x)
 def test_heap_minimum(self):
     L1 = linked_list(1)
     L1.insert(linked_list_node(1))
     L1.insert(linked_list_node(1))
     L2 = linked_list(2)
     L2.insert(linked_list_node(2))
     L2.insert(linked_list_node(2))
     L3 = linked_list(3)
     L3.insert(linked_list_node(3))
     L3.insert(linked_list_node(3))
     L4 = linked_list(4)
     L4.insert(linked_list_node(4))
     L5 = linked_list(5)
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     L3.insert(linked_list_node(5))
     q = min_priority_queue([L1, L2, L3, L4, L5])
     self.assertEquals(q.heap_minimum().key, 1)
 def test_first_insert_method(self):
   '''
   when first insert is called, head and tail should equal
   and the data stored in head should be equal to the data inserted
   '''
   ll = linked_list()
   d = 'arbitrary data'
   ll.insert(d) #insert arbitrary data into the list
   self.assertEqual(ll.head, ll.tail)
   self.assertEqual(ll.head.data, d)
Esempio n. 24
0
 def testPop(self):
     """pop should give a known result with a known input"""
     item1 = 5
     item2 = 10
     l2 = linked_list()
     l2.insert(item1)
     l2.insert(item2)
     x = l2.pop()
     self.assertEqual(x, item2)
     self.assertEqual(1, l2.size())
def build_populated_list():
    item6 = node()
    item5 = node(1, item6)
    item4 = node('dog', item5)
    item3 = node(45, item4)
    item2 = node('octopus', item3)
    item1 = node(37, item2)
    populated_list = linked_list()
    populated_list.head = item1
    return populated_list, item1, item2, item3, item4, item5
 def test_first_insert_method(self):
     '''
 when first insert is called, head and tail should equal
 and the data stored in head should be equal to the data inserted
 '''
     ll = linked_list()
     d = 'arbitrary data'
     ll.insert(d)  #insert arbitrary data into the list
     self.assertEqual(ll.head, ll.tail)
     self.assertEqual(ll.head.data, d)
Esempio n. 27
0
 def testPrint(self):
     item1 = 5
     item2 = 10
     item3 = "three"
     l3 = linked_list()
     l3.insert(item1)
     l3.insert(item2)
     l3.insert(item3)
     x = l3.print_list()
     y = tuple(["three", 10, 5])
     self.assertEqual(x, y)
Esempio n. 28
0
    def __init__(self, directory='some_text.txt', line_am=5):
        self.line_am = line_am
        self.dir = directory
        if not os.path.exists(directory):
            open(directory, 'bw').close()

        self.data = linked_list([0, os.path.getsize(directory)])
        self.page_b = list()
        self.page_s = list()
        self.current_page = 0
        self._load()
Esempio n. 29
0
 def save(self):
     with open(self.dir, 'rb') as old:
         with open('new.txt', 'wb') as new:
             for r in linked_list.range(0, float('inf')):
                 old.seek(r[0], 0)
                 new.write(old.read(r[1] - r[0]))
     os.remove(self.dir)
     os.renames('new.txt', self.dir)
     linked_list.head = None
     self.data = linked_list([0, os.path.getsize(self.dir)])
     self._load()
Esempio n. 30
0
def test_stack():
    s = linked_list()
    s.push("elem 1")
    s.push("elem 2")
    s.push("2")
    eq_(s.head(), "2", print("head method test passed"))
    s.pop()  #test method on lists
    eq_(s.size(), 2, print("push and pop test passed"))
    s.pop()
    s.pop()
    eq_(s.is_empty(), True, print("is_empty method test passed"))
Esempio n. 31
0
def run():
    dupes_list = ['m', 'q', 'g', 'z', 'o', 'a', 'u', 'e', 'a', 'm', 's']
    dupes_ll = linked_list()

    for e in dupes_list[1:]:
        dupes_ll.set_data(e)
        dupes_ll.set_next(linked_list())
        dupes_ll = dupes_ll.get_next()

    non_dupes = remove_dupes_buffer(dupes_ll)
    print("%s\n-------------\n" % non_dupes)

    dupes_ll = linked_list(dupes_list[0])

    for e in dupes_list[1:]:
        new_node = linked_list(e)
        dupes_ll.set_next(new_node)
        dupes_ll = new_node

    non_dupes = remove_dupes_no_buffer(dupes_ll)
    print("%s\n-------------\n" % non_dupes)
 def test_view_all_method(self):
   '''
   make sure the view_all method returns a list of all data in the correct order
   '''
   ll = linked_list()
   first_datum = 'first'
   second_datum = 'second'
   third_datum = 'third'
   ll.insert(first_datum)
   ll.insert(second_datum)
   ll.insert(third_datum)
   self.assertEqual(ll.view_all(), [third_datum, second_datum, first_datum])
Esempio n. 33
0
 def test_search(self):
     L = linked_list()
     a = linked_list_node(1)
     b = linked_list_node(4)
     c = linked_list_node(16)
     d = linked_list_node(9)
     e = linked_list_node(25)
     L.insert(a)
     L.insert(b)
     L.insert(c)
     L.insert(d)
     L.insert(e)
     self.assertEquals(L.search(4), b)
 def test_view_all_method(self):
     '''
 make sure the view_all method returns a list of all data in the correct order
 '''
     ll = linked_list()
     first_datum = 'first'
     second_datum = 'second'
     third_datum = 'third'
     ll.insert(first_datum)
     ll.insert(second_datum)
     ll.insert(third_datum)
     self.assertEqual(ll.view_all(),
                      [third_datum, second_datum, first_datum])
Esempio n. 35
0
 def test_search(self):
     L = linked_list()
     a = linked_list_node(1)
     b = linked_list_node(4)
     c = linked_list_node(16)
     d = linked_list_node(9)
     e = linked_list_node(25)
     L.insert(a)
     L.insert(b)
     L.insert(c)
     L.insert(d)
     L.insert(e)
     self.assertEqual(L.search(4), b)
Esempio n. 36
0
def generate_bin(int_val):
    """
    """
    obj = linked_list.linked_list()
    while True:
        rem = (int_val/2)
        mod = (int_val % 2)
        int_val = rem
        obj.add_node(mod)
        print 'mod: ', mod, 'rem', rem, 'int_val', int_val
        if int_val == 0:
            break

    print obj.list_print()
 def test_second_insert(self):
   '''
   after the second insert, the head should point to the tail
   '''
   ll = linked_list()
   first_datum = 'the first bit of arbitrary data'
   second_datum = 'the second bit of arbitrary data'
   ll.insert(first_datum)
   ll.insert(second_datum)
   # head.next should point to tail
   self.assertEqual(ll.head.next, ll.tail)
   # head.data should be the last data inserted, tail should be first
   self.assertEqual(ll.head.data, second_datum)
   self.assertEqual(ll.head.next.data, first_datum)
Esempio n. 38
0
 def test_insert(self):
     L = linked_list()
     a = linked_list_node(1)
     b = linked_list_node(4)
     c = linked_list_node(16)
     d = linked_list_node(9)
     e = linked_list_node(25)
     L.insert(a)
     L.insert(b)
     L.insert(c)
     L.insert(d)
     L.insert(e)
     l = []
     x = L.head
     while x != None:
         l.append(x)
         x = x.next
     self.assertEquals(l, [e, d, c, b, a])
 def test_linked_list_initialization(self):
   ll = linked_list()
   self.assertIsNotNone(ll.head)
   self.assertIsNone(ll.tail)
def build_empty_list():
    return linked_list()
Esempio n. 41
0
from linked_list import linked_list

lis = linked_list(100)
lis.add_node(200)
lis.print_list()


 def testPop(self):
     lst = linked_list()
     lst.insert(1)
     lst.pop()
     self.assertFalse(False, lst.size())
 def testRemoveSingle(self):
     lst = linked_list()
     lst.insert(1)
     lst.remove(lst.head)
     self.assertEqual(None, lst.head)
 def testPrint(self):
     lst = linked_list()
     self.assertFalse(0, lst.print_list())
 def testInsert(self):
     lst = linked_list()
     lst.insert(1)
     self.assertFalse(0, lst.size())
 def testSearch(self):
     item = 1
     ls2 = linked_list()
     ls2.insert(item)
     nd2 = ls2.search(item)
     self.assertEqual(nd2.data, item)