Esempio n. 1
0
 def test_node_repr(self):
     a = sllist([None]).first
     self.assertEqual(repr(a), '<sllistnode(None)>')
     b = sllist([1, None]).first
     self.assertEqual(repr(b), '<sllistnode(1)>')
     c = sllist(['abc', None]).first
     self.assertEqual(repr(c), '<sllistnode(\'abc\')>')
Esempio n. 2
0
def Tiling(text1,text2): #return "Empty" if tiling is not possible
	resultantString=""
	text1_words=text1.split(" ")
	text2_words=text2.split(" ")
	text1_list=sllist()
	text2_list=sllist()
	resultList1=sllist()
	resultList2=sllist
	for i in range(len(text1_words)):
		text1_list.append(text1_words[i])
	for i in range(len(text2_words)):
		text2_list.append(text2_words[i])
	resultantString1=""
	resultantString2=""
	resultList1=Join(text1_list,text2_list)
	resultList2=Join(text2_list,text1_list)
	if(resultList1.size!=0):
		for i in resultList1:
			resultantString1=resultantString1+i
			resultantString1=resultantString1+" "
	else:
		resultantString1=""
	#------------------------
	if(resultList2.size!=0):
		for i in resultList2:
			resultantString2=resultantString2+i
			resultantString2=resultantString2+" "
	else:
		resultantString2=""
	if(len(resultantString1)>len(resultantString2)):
		return resultantString1
	elif(len(resultantString2)>len(resultantString1)):
		return resultantString2
	else:
		return ""
Esempio n. 3
0
def intersect_LL(arr_a, arr_b):
    """
    this solution only works if there are no duplicate elements.
    Each pointer traverses 1st one list then the other total length O(m+n).
     If there are no intersections, then they will both reach the end together
    :param arr_a:
    :param arr_b:
    :return:
    """
    if (arr_a or arr_b) is None or len(arr_a) == 0 or len(arr_b) == 0:
        return -1
    sll_a, sll_b = sllist(arr_a), sllist(arr_b)
    pa = sll_a.first
    pb = sll_b.first
    while pa.value is not pb.value:
        pa = pa.next
        pb = pb.next
        if pa is None and pb is None:
            return -1

        if pa is None:
            pa = sll_b.first

        if pb is None:
            pb = sll_a.first

    return pa.value
Esempio n. 4
0
 def test_node_str(self):
     a = sllist([None, None]).first
     self.assertEqual(str(a), 'sllistnode(None)')
     b = sllist([1, None]).first
     self.assertEqual(str(b), 'sllistnode(1)')
     c = sllist(['abc', None]).first
     self.assertEqual(str(c), 'sllistnode(abc)')
Esempio n. 5
0
 def test_appendleft(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([10, 0, 1, 2, 3])
     next = ll.nodeat(0)
     arg_node = sllistnode(10)
     new_node = ll.appendleft(arg_node)
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(ll.first, new_node)
     self.assertEqual(ll, ref)
Esempio n. 6
0
 def test_insert_node_before_first(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([10, 0, 1, 2, 3])
     next = ll.nodeat(0)
     arg_node = sllistnode(10)
     new_node = ll.insertnodebefore(arg_node, ll.nodeat(0))
     self.assertEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(new_node, ll.first)
     self.assertEqual(ll, ref)
Esempio n. 7
0
 def test_appendright(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([0, 1, 2, 3, 10])
     prev = ll.nodeat(-1)
     arg_node = sllistnode(10)
     new_node = ll.appendright(arg_node)
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, None)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(ll.last, new_node)
     self.assertEqual(ll, ref)
Esempio n. 8
0
 def test_insert_value_after(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([0, 1, 2, 10, 3])
     prev = ll.nodeat(2)
     next = ll.nodeat(3)
     arg_node = sllistnode(10)
     new_node = ll.insertafter(arg_node, ll.nodeat(2))
     self.assertNotEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(ll, ref)
Esempio n. 9
0
 def test_insert_node_after_last(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([0, 1, 2, 3, 10])
     prev = ll.nodeat(3)
     arg_node = sllistnode(10)
     new_node = ll.insertnodeafter(arg_node, ll.nodeat(-1))
     self.assertEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, None)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(new_node, ll.last)
     self.assertEqual(ll, ref)
Esempio n. 10
0
 def test_concat(self):
     a_ref = py23_range(0, 1024, 4)
     a = sllist(a_ref)
     b_ref = py23_range(8092, 8092 + 1024, 4)
     b = sllist(b_ref)
     ab_ref = sllist(a_ref + b_ref)
     c = a + b
     self.assertEqual(c, ab_ref)
     self.assertEqual(len(c), len(ab_ref))
     c = a + b_ref
     self.assertEqual(c, ab_ref)
     self.assertEqual(len(c), len(ab_ref))
Esempio n. 11
0
 def test_insert_node_before_another(self):
     ll = sllist(py23_xrange(4))
     ref = sllist([0, 1, 10, 2, 3])
     prev = ll.nodeat(1)
     next = ll.nodeat(2)
     arg_node = sllistnode(10)
     new_node = ll.insertnodebefore(arg_node, ll.nodeat(2))
     self.assertEqual(new_node, arg_node)
     self.assertEqual(new_node.value, 10)
     self.assertEqual(new_node.next, next)
     self.assertEqual(prev.next, new_node)
     self.assertEqual(ll, ref)
Esempio n. 12
0
 def test_concat_empty(self):
     empty = sllist()
     filled_ref = py23_range(0, 1024, 4)
     filled = sllist(filled_ref)
     res = empty + empty
     self.assertEqual(res, sllist([] + []))
     self.assertEqual(len(res), 0)
     res = empty + filled
     self.assertEqual(res, sllist([] + filled_ref))
     self.assertEqual(len(res), len(filled_ref))
     res = filled + empty
     self.assertEqual(res, sllist(filled_ref + []))
     self.assertEqual(len(res), len(filled_ref))
Esempio n. 13
0
def merge_infinite(list_of_lists):
    """
    This is different from the above method in that it uses a linked-list under the hood
    It also uses fixed-windows of size Nk elements and merges them before sliding to the next
    non-overlapping window. The size of priority queue is no greater than k at any given point

    A last pass is required to merge all the windows
    We show the computation for 1 window only
    :param list_of_lists:
    :return:
    """
    K = len(list_of_lists)
    N = len(list_of_lists[0])
    if list_of_lists is None or len(list_of_lists) == 0:
        return list_of_lists
    h = []
    out = []
    list_of_linked_lists = []
    for lst in list_of_lists:
        # converts each of the k-lists into a singly-linked-list
        # total time = O(Nk)
        list_of_linked_lists.append(sllist(lst))

    while len(out) != N * K:
        # push upto k elements at a time into pq. Takes O(lg k)
        for k in xrange(K):
            if list_of_linked_lists[k].first is not None:
                # remove the head of corresponding linked list and advance its pointer
                # this is an O(1) operation
                heapq.heappush(h, list_of_linked_lists[k].popleft())

        item = heapq.heappop(h)  # this is an O(1) operation in min-heap
        out.append(item)

    return out
Esempio n. 14
0
    def test_iternodes_with_removed_node(self):
        ll = sllist(['x', 'removed item'])
        removed_node = ll.last

        for node in ll.iternodes():
            self.assertNotEqual(node, removed_node)
            ll.remove(removed_node)
Esempio n. 15
0
def bind(name, handler):
    """
    Bind a handler to a named event.
    :param name: Event name to trigger
    :type name: str | unicode
    :param handler: Handler for event
    :type handler: (*args, **kwargs) -> None
    :return: Nothing yet
    :rtype: None
    :raises TypeError: if name is not an string or handler is not callable
    :raises ValueError: if name contains a blank string (empty or whitespace)
    """
    global _events
    name = _event_name(name)
    if not callable(handler):
        raise TypeError('handler parameter must be callable!')
    elif name not in _events:
        _debug('Constructing new handler list for event: %s', name)
        _events[name] = sllist()

    # Avoid duplicate handlers
    handlers = _events[name]
    if handler not in handlers:
        _debug('Binding handler for "%s" event: %s', name, repr(handler))
        handlers.append(handler)
Esempio n. 16
0
 def test_rotate_right_empty(self):
     for n in py23_xrange(4):
         ll = sllist()
         ll.rotate(n)
         self.assertEqual(ll.first, None)
         self.assertEqual(ll.last, None)
         self.assertEqual(ll.size, 0)
Esempio n. 17
0
def easy():
    global input

    recipes = sllist([3, 7])
    first_elf = recipes.first
    second_elf = recipes.first.next
    num_recipes = len(recipes)

    while num_recipes < input + 10:
        s = first_elf.value + second_elf.value
        for r in str(s):
            r = int(r)
            recipes.appendright(r)
            num_recipes += 1
        for _ in range(first_elf.value + 1):
            first_elf = first_elf.next
            if first_elf is None:
                first_elf = recipes.first
        for _ in range(second_elf.value + 1):
            second_elf = second_elf.next
            if second_elf is None:
                second_elf = recipes.first
    for _ in range(input):
        recipes.popleft()
    print("".join([str(r) for r in recipes])[:10])
Esempio n. 18
0
def add_list_reversed(list1: sllist, list2: sllist) -> sllist:
    st1, st2, st3 = [], [], []
    counter1, counter2, carry = list1.first, list2.first, 0
    flist = sllist()

    while counter1:
        st1.append(counter1.value)
        counter1 = counter1.next

    while counter2:
        st2.append(counter2.value)
        counter2 = counter2.next

    while st1 and st2:
        add = st1.pop() + st2.pop() + carry
        carry = add // 10
        add %= 10

        st3.append(add)

    while st1:
        st3.append(st1.pop() + carry)
        carry = 0

    while st2:
        st3.append(st2.pop() + carry)
        carry = 0

    if carry == 1:
        flist.appendright(1)

    while st3:
        flist.appendright(st3.pop())

    return flist
Esempio n. 19
0
 def test_node_owner(self):
     ll = sllist([1234])
     owner_ref = ll.first.owner
     self.assertIsInstance(owner_ref, weakref.ref)
     self.assertIs(owner_ref(), ll)
     del ll
     self.assertIsNone(owner_ref())
Esempio n. 20
0
def bind(name, handler):
    """
    Bind a handler to a named event.
    :param name: Event name to trigger
    :type name: str | unicode
    :param handler: Handler for event
    :type handler: (*args, **kwargs) -> None
    :return: Nothing yet
    :rtype: None
    :raises TypeError: if name is not an string or handler is not callable
    :raises ValueError: if name contains a blank string (empty or whitespace)
    """
    global _events
    name = _event_name(name)
    if not callable(handler):
        raise TypeError('handler parameter must be callable!')
    elif name not in _events:
        _debug('Constructing new handler list for event: %s', name)
        _events[name] = sllist()

    # Avoid duplicate handlers
    handlers = _events[name]
    if handler not in handlers:
        _debug('Binding handler for "%s" event: %s', name, repr(handler))
        handlers.append(handler)
Esempio n. 21
0
 def test_remove_from_n_elem(self):
     ll = sllist()
     nn = sllistnode()
     ll.append(nn)
     to_del = ll.nodeat(0)
     ll.remove(to_del)
     self.assertEqual(None, None)
Esempio n. 22
0
def sll_find_mid(arr):
    """
    Keep a slow pointer and a fast pointer that runs at twice the speed.
    When the fast pointer reaches the end, the slow one is at the mid-point
    :param arr: Input list
    :return:
    """
    if arr is None or len(arr) == 0:
        return None

    sll = sllist(arr)
    slow = fast = sll.first

    if len(sll) in (1,2):
        return sll[-1]

    # import ipdb; ipdb.set_trace()
    while fast:
        if fast.next:
            slow = slow.next
            fast = fast.next.next
        else:
            break

    return slow.value
Esempio n. 23
0
 def test_clear_empty(self):
     empty_list = sllist()
     empty_list.clear()
     self.assertEqual(empty_list.first, None)
     self.assertEqual(empty_list.last, None)
     self.assertEqual(empty_list.size, 0)
     self.assertEqual(list(empty_list), [])
Esempio n. 24
0
def merge_infinite(list_of_lists):
    """
    This is different from the above method in that it uses a linked-list under the hood
    It also uses fixed-windows of size Nk elements and merges them before sliding to the next
    non-overlapping window. The size of priority queue is no greater than k at any given point

    A last pass is required to merge all the windows
    We show the computation for 1 window only
    :param list_of_lists:
    :return:
    """
    K = len(list_of_lists)
    N = len(list_of_lists[0])
    if list_of_lists is None or len(list_of_lists) == 0:
        return list_of_lists
    h = []
    out = []
    list_of_linked_lists = []
    for lst in list_of_lists:
        # converts each of the k-lists into a singly-linked-list
        # total time = O(Nk)
        list_of_linked_lists.append(sllist(lst))

    while len(out) != N * K:
        # push upto k elements at a time into pq. Takes O(lg k)
        for k in xrange(K):
            if list_of_linked_lists[k].first is not None:
                # remove the head of corresponding linked list and advance its pointer
                # this is an O(1) operation
                heapq.heappush(h, list_of_linked_lists[k].popleft())

        item = heapq.heappop(h)  # this is an O(1) operation in min-heap
        out.append(item)

    return out
Esempio n. 25
0
 def test_lt(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertFalse(sllist() < sllist())
     self.assertFalse(a < a)
     self.assertTrue(sllist() < a)
     self.assertFalse(a < sllist())
     self.assertTrue(a < b)
     self.assertFalse(b < a)
     self.assertTrue(c < d)
     self.assertFalse(d < c)
     self.assertFalse(e < f)
     self.assertTrue(f < e)
Esempio n. 26
0
def radixSort(Arr):
    #find max value in the array
    maxVal = max(Arr)
    digits = len(str(maxVal))  # find total digits in the max value
    sll = sllist()
    for i in range(0, digits):
        distribute(Arr, i, sll)
        collect(Arr, sll)
Esempio n. 27
0
 def test_eq(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertTrue(sllist() == sllist())
     self.assertTrue(a == a)
     self.assertFalse(sllist() == a)
     self.assertFalse(a == sllist())
     self.assertFalse(a == b)
     self.assertFalse(b == a)
     self.assertFalse(c == d)
     self.assertFalse(d == c)
     self.assertFalse(e == f)
     self.assertFalse(f == e)
Esempio n. 28
0
 def test_ne(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertFalse(sllist() != sllist())
     self.assertFalse(a != a)
     self.assertTrue(sllist() != a)
     self.assertTrue(a != sllist())
     self.assertTrue(a != b)
     self.assertTrue(b != a)
     self.assertTrue(c != d)
     self.assertTrue(d != c)
     self.assertTrue(e != f)
     self.assertTrue(f != e)
Esempio n. 29
0
 def test_gt(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertFalse(sllist() > sllist())
     self.assertFalse(a > a)
     self.assertFalse(sllist() > a)
     self.assertTrue(a > sllist())
     self.assertFalse(a > b)
     self.assertTrue(b > a)
     self.assertFalse(c > d)
     self.assertTrue(d > c)
     self.assertTrue(e > f)
     self.assertFalse(f > e)
Esempio n. 30
0
 def test_le(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertTrue(sllist() <= sllist())
     self.assertTrue(a <= a)
     self.assertTrue(sllist() <= a)
     self.assertFalse(a <= sllist())
     self.assertTrue(a <= b)
     self.assertFalse(b <= a)
     self.assertTrue(c <= d)
     self.assertFalse(d <= c)
     self.assertFalse(e <= f)
     self.assertTrue(f <= e)
Esempio n. 31
0
 def test_ge(self):
     a = sllist(py23_xrange(0, 1100))
     b = sllist(py23_xrange(0, 1101))
     c = sllist([1, 2, 3, 4])
     d = sllist([1, 2, 3, 5])
     e = sllist([1, 0, 0, 0])
     f = sllist([0, 0, 0, 0])
     self.assertTrue(sllist() >= sllist())
     self.assertTrue(a >= a)
     self.assertFalse(sllist() >= a)
     self.assertTrue(a >= sllist())
     self.assertFalse(a >= b)
     self.assertTrue(b >= a)
     self.assertFalse(c >= d)
     self.assertTrue(d >= c)
     self.assertTrue(e >= f)
     self.assertFalse(f >= e)
Esempio n. 32
0
 def test_cmp_nonlist(self):
     a = sllist(py23_xrange(0, 1100))
     b = [py23_xrange(0, 1100)]
     if sys.hexversion < 0x03000000:
         # actual order is not specified by language
         self.assertNotEqual(cmp(a, b), 0)
         self.assertNotEqual(cmp(b, a), 0)
         self.assertNotEqual(cmp([], a), 0)
         self.assertNotEqual(cmp(a, []), 0)
def get_max_path(list1, list2):
    final_list = sllist()
    templist1 = sllist()
    templist2 = sllist()
    node1 = list1.first
    node2 = list2.first
    sum1 = sum2 = 0
    
    while node1 and node2:
        if node1.value == node2.value:
            if sum1 > sum2:
                final_list.extend(templist1) 
            else:
                final_list.extend(templist2)
            final_list.append(node1)
            templist1 = sllist()
            templist2 = sllist()
            node1 = node1.next      
            node2 = node2.next     
            sum1 = sum2 = 0
        elif node1.value < node2.value:
            templist1.append(node1)
            sum1 += node1.value
            node1 = node1.next
        elif node2.value < node1.value:
            templist2.append(node2)
            sum2 += node2.value
            node2 = node2.next
    while node1:
        final_list.append(node1)
        node1 = node1.next
    while node2:
        final_list.append(node2)
        node2 = node2.next
                        
    return final_list
Esempio n. 34
0
	def sortData(self, sortIndex):
		#setup empty list
		empty_lst = sllist()
		#fill the list
		print("The length of the input is: ",len(self.x))
		for i in range (0,len(self.x)):
			empty_lst.append([self.x[i],self.y[i]])
		length = len(empty_lst)
		print("The length of the list is: ",length)
		print("The unsorted list is: ",empty_lst)
		#run bubble sort
		for i in range(len(empty_lst)):
			for j in range(len(empty_lst)-1-i):
				if empty_lst[j][sortIndex] > empty_lst[j+1][sortIndex]:
					empty_lst[j][sortIndex], empty_lst[j+1][sortIndex] = empty_lst[j+1][sortIndex], empty_lst[j][sortIndex] #swap
		print("The sorted list is: ",empty_lst)
Esempio n. 35
0
def initMap():
    map = sllist()
    return map
def main():
    list1 = sllist([1, 3, 30, 90, 120, 240, 511])
    list2 = sllist([0, 3, 12, 32, 90, 125, 240, 249])
    print get_max_path(list1, list2)