コード例 #1
0
def _check_func(func, left, right, expected):
    left_list = linkedlist.create_linkedlist(left)
    right_list = linkedlist.create_linkedlist(right)
    expected_list = linkedlist.create_linkedlist(expected)
    actual = func(left_list, right_list)
#    print '{} == {}'.format(actual, expected_list)
    if str(actual) != str(expected_list):
        raise AssertionError(
            'FAIL: Expected: {}   Actual: {}   Input left: {}  Input right: {}'.format(
                expected_list, actual, left, right))
コード例 #2
0
def _check_func(func, left, right, expected):
    left_list = linkedlist.create_linkedlist(left)
    right_list = linkedlist.create_linkedlist(right)
    expected_list = linkedlist.create_linkedlist(expected)
    actual = func(left_list, right_list)
    #    print '{} == {}'.format(actual, expected_list)
    if str(actual) != str(expected_list):
        raise AssertionError(
            'FAIL: Expected: {}   Actual: {}   Input left: {}  Input right: {}'
            .format(expected_list, actual, left, right))
コード例 #3
0
def _int_to_list(the_int):
    if not the_int:
        return linkedlist.create_linkedlist([0])
    else:
        result = []
        remaining_int = the_int
        while remaining_int != 0:
            digit = remaining_int % 10
            result.append(digit)
            remaining_int = int(remaining_int / 10)
    return linkedlist.create_linkedlist(result)
コード例 #4
0
def _int_to_list(the_int):
    if not the_int:
        return linkedlist.create_linkedlist([0])
    else:
        result = []
        remaining_int = the_int
        while remaining_int != 0:
            digit = remaining_int % 10
            result.append(digit)
            remaining_int = int(remaining_int/10)
    return linkedlist.create_linkedlist(result)
コード例 #5
0
def _test_linked_list():
    import linkedlist  # Localize this import since it's only for this test. (Violating PEP-8.)
    test_list = linkedlist.create_linkedlist([0, 1, 1, 2, 3, 3, 1])
    assert str(test_list) == '[0, 1, 1, 2, 3, 3, 1]'
    remove_duplicates_linkedlist(test_list)
    assert str(test_list) == '[0, 2, 3, 1]'
    print 'PASS {}'.format(remove_duplicates_linkedlist)
コード例 #6
0
def _test_all():
    test_list = linkedlist.create_linkedlist([0, 1, 2])
    node = test_list.head.next
    assert node.data == 1
    delete_node(node)
    assert str(test_list) == '[0, 2]'
    print 'SUCCESS'
コード例 #7
0
def _test_linked_list():
    import linkedlist # Localize this import since it's only for this test. (Violating PEP-8.)
    test_list = linkedlist.create_linkedlist([0,1,1,2,3,3,1])
    assert str(test_list) == '[0, 1, 1, 2, 3, 3, 1]'
    remove_duplicates_linkedlist(test_list)
    assert str(test_list) == '[0, 2, 3, 1]'
    print 'PASS {}'.format(remove_duplicates_linkedlist)
コード例 #8
0
def _test_all():
    test_list = linkedlist.create_linkedlist([0, 1, 2])
    node = test_list.head.next
    assert node.data == 1
    delete_node(node)
    assert str(test_list) == '[0, 2]'
    print 'SUCCESS'
コード例 #9
0
def _test_cycle(func, pylist, start_val, end_val):
    the_list = linkedlist.create_linkedlist(pylist)
    # Create a cycle.
    start = the_list.find_node(start_val)
    end = the_list.find_node(end_val)
    end.next = start
    _check_func(func, the_list, start)
コード例 #10
0
def _test(func, input):
    expected = input[::-1]
    input_str = str(input)
    input = linkedlist.create_linkedlist(input)
    actual = func(input)
    print '{}    ====>   {}'.format(input_str, actual)
    if str(actual) != str(expected):
        raise Exception('FAIL Expected: {}   Actual:  {}'.format(expected, actual))
コード例 #11
0
def sum_lists(ll1, ll2, reverse=True):
	if reverse:
		head1 = ll1
		head2 = ll2
		current1 = head1
		current2 = head2
		carry = 0
		prev = None

		while current1 is not None:
			if current2 is not None:
				sm = current1.value + current2.value + carry
				carry, current1.value = sm // 10, sm % 10
				prev = current1
				current1, current2 = current1.next, current2.next

		while current2 is not None:
			prev.next = Node()
			current1 = prev.next
			current1.value = current2.value + carry
			prev = current2
			current2 = current2.next

		if carry != 0:
			prev.next = Node()
			prev.next.value = carry

		return head1

	else:
		ll1_str = ''
		ll2_str = ''

		while ll1 is not None:
			ll1_str += str(ll1.value)
			ll1 = ll1.next
		while ll2 is not None:
			ll2_str += str(ll2.value)
			ll2 = ll2.next

		result_str = str(int(ll1_str) + int(ll2_str))

		return create_linkedlist(result_str)
コード例 #12
0
		ll2_str = ''

		while ll1 is not None:
			ll1_str += str(ll1.value)
			ll1 = ll1.next
		while ll2 is not None:
			ll2_str += str(ll2.value)
			ll2 = ll2.next

		result_str = str(int(ll1_str) + int(ll2_str))

		return create_linkedlist(result_str)


if __name__ == '__main__':
	ll1r = create_linkedlist([7, 1, 6])
	ll2r = create_linkedlist([5, 9, 2])

	print("ll1")
	print_linkedlist(ll1r)
	print("ll2")
	print_linkedlist(ll2r)
	print("sum")
	print_linkedlist(sum_lists(ll1r, ll2r))

	ll1 = create_linkedlist([7, 1, 6][::-1])
	ll2 = create_linkedlist([5, 9, 2][::-1])

	print("ll1")
	print_linkedlist(ll1)
	print("ll2")
コード例 #13
0
	if index == k:
		print("{0}th element is {1}".format(k, head.value))

	return index

def kth_to_last(head, k):
	if head is None:
		raise ValueError("Linkedlist doesn't exist.")

	length = 0
	current = head

	while current is not None:
		current = current.next
		length += 1

	pos = length - k - 1
	current = head
	i = 0

	while current is not None:
		if i == pos:
			return current.value

		current = current.next
		i += 1
		
if __name__ == '__main__':
	ll = create_linkedlist([1, 2, 3, 5, 12, 4])

	print(kth_to_last(ll, 6))
コード例 #14
0
                prev.next = current.next
                current = current.next

        else:
            prev = current
            current = current.next

    while greater:
        element = greater.pop(0)
        current.value = element
        current.next = Node()
        prev = current
        current = current.next

    return header


if __name__ == '__main__':
    lst = [3, 5, 8, 5, 10, 2, 1]
    ll = create_linkedlist(lst)
    pivot = 5

    print("List")
    print(lst)
    print("Linkedlist")
    print_linkedlist(ll)

    print("Linkedlist partitioned")
    ll_p = partition(ll, 5)
    print_linkedlist(ll)
コード例 #15
0
            if p2.value == p1.value:
                prev.next = p2.next
                p2 = p2.next

            else:
                prev = p2
                p2 = p2.next

        p1 = p1.next

        try:
            p2 = p1.next
        except AttributeError:
            prev.next = Node()
            return head
        else:
            prev = p1

    return head


if __name__ == '__main__':
    ll1 = create_linkedlist(
        [1, 1, None, None, 3, 5, 2, 3, 4, 1, 5, 3, 1, 5, 2])
    ll2 = create_linkedlist([1, None, 3, 5, 2, 4])

    assert linkedlist_equal(remove_dups(ll1), ll2)
    assert linkedlist_equal(remove_dups_no_buffer(ll1), ll2)

    print("All tests passed.")
コード例 #16
0
from linkedlist import Node, create_linkedlist, linkedlist_equal, print_linkedlist


def palindrome(ll):
	if ll is None:
		return True

	result_str = ''

	while ll is not None:
		result_str += str(ll.value)
		ll = ll.next

	return result_str == result_str[::-1]


if __name__ == '__main__':
	ll1 = create_linkedlist([1, 2, 3, 2, 1])
	ll2 = create_linkedlist([1, 2, 3, 2, 1, 1, 1])
	ll3 = create_linkedlist([1, 2, 3, 3, 2, 1])

	assert palindrome(ll1)
	assert not palindrome(ll2)
	assert palindrome(ll3)

	print("All tests passed.")
コード例 #17
0
ファイル: s02find.py プロジェクト: 14Si28/coding_interview
def _create_linkedlist(pylist):
    test_list = linkedlist.create_linkedlist(pylist)
    assert str(test_list) == str(pylist)
    return test_list
コード例 #18
0
def _create_linkedlist(pylist):
    test_list = linkedlist.create_linkedlist(pylist)
    assert str(test_list) == str(pylist)
    return test_list