Exemplo n.º 1
0
def get_odd_even_list():
    li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
    link = DataTransformat.list_2_link(li)

    new_link = Solution().oddEvenList(link)
    new_li = DataTransformat.link_2_list(new_link)
    print(new_li)
Exemplo n.º 2
0
def test_mergeKLists():
    data = Solution().mergeKLists([
        DataTransformat.list_2_link([1, 4, 5]),
        DataTransformat.list_2_link([1, 3, 4]),
        DataTransformat.list_2_link([2, 6])
    ])
    print(DataTransformat.link_2_list(data))
Exemplo n.º 3
0
def test_reorderList():
    res1 = DataTransformat.link_2_list(Solution().reorderList(
        DataTransformat.list_2_link([1, 2, 3, 4])))
    assert res1 == [1, 4, 2, 3]

    res2 = DataTransformat.link_2_list(Solution().reorderList(
        DataTransformat.list_2_link([1, 2, 3, 4, 5])))
    assert res2 == [1, 5, 2, 4, 3]
Exemplo n.º 4
0
def merge2list():
    l1 = [1, 2, 4, 5, 7]
    l2 = [3, 6, 9, 10]
    link1 = DataTransformat.list_2_link(l1)
    link2 = DataTransformat.list_2_link(l2)
    res = Solution().mergeTwoLists(link1, link2)
    res_list = DataTransformat.link_2_list(res)
    print(res_list)
Exemplo n.º 5
0
def add_2_numbers():
    list_1 = [2, 4, 3]
    link_1 = DataTransformat.list_2_link(list_1)

    list_2 = [5, 6, 4]
    link_2 = DataTransformat.list_2_link(list_2)

    res_link = Solution().addTwoNumbers(link_1, link_2)
    res_list = DataTransformat.link_2_list(res_link)
    print(res_list)
Exemplo n.º 6
0
def get_reverseBetween():
    li = [1, 2, 3, 4, 5]
    link = DataTransformat.list_2_link(li)
    m = 2
    n = 4
    with Timer() as t:
        res = Solution().reverseBetween(link, m, n)
    res_list = DataTransformat.link_2_list(res)
    print(res_list)
    print(f"cost time {t.elapsed}")
def get_next_larger_nodes():
    origin_list1 = [2, 1, 5]
    origin_list2 = [2, 7, 4, 3, 5]
    origin_list3 = [1, 7, 5, 1, 9, 2, 5, 1]
    origin_link1 = DataTransformat.list_2_link(origin_list1)
    origin_link2 = DataTransformat.list_2_link(origin_list2)
    origin_link3 = DataTransformat.list_2_link(origin_list3)
    print(Solution.nextLargerNodes(origin_link1))
    print(Solution.nextLargerNodes(origin_link2))
    print(Solution.nextLargerNodes(origin_link3))
Exemplo n.º 8
0
def test_remove_elements():
    li1, del1 = [6], 6
    li2, del2 = [1, 2, 3, 4, 5], 3
    li3, del3 = [1, 2, 3, 4, 5], 6
    li4, del4 = [], 0

    link1 = DataTransformat.list_2_link(li1)
    link2 = DataTransformat.list_2_link(li2)
    link3 = DataTransformat.list_2_link(li3)
    link4 = DataTransformat.list_2_link(li4)

    with exec_time():
        res1 = Solution.removeElements(link1, del1)
        res2 = Solution.removeElements(link2, del2)
        res3 = Solution.removeElements(link3, del3)
        res4 = Solution.removeElements(link4, del4)

    show_res1 = DataTransformat.link_2_list(res1)
    show_res2 = DataTransformat.link_2_list(res2)
    show_res3 = DataTransformat.link_2_list(res3)
    show_res4 = DataTransformat.link_2_list(res4)

    print(show_res1)
    print(show_res2)
    print(show_res3)
    print(show_res4)
Exemplo n.º 9
0
def test_isPalindrome():
    li1 = [1, 2, 3, 2, 1]
    li2 = [2]
    li3 = [1, 2, 3, 4]
    li4 = [1, 2, 3, 3, 2, 1]

    link1 = DataTransformat.list_2_link(li1)
    link2 = DataTransformat.list_2_link(li2)
    link3 = DataTransformat.list_2_link(li3)
    link4 = DataTransformat.list_2_link(li4)

    with exec_time():
        print(Solution.isPalindrome1(link1))
        print(Solution.isPalindrome1(link2))
        print(Solution.isPalindrome1(link3))
        print(Solution.isPalindrome1(link4))

    link1 = DataTransformat.list_2_link(li1)
    link2 = DataTransformat.list_2_link(li2)
    link3 = DataTransformat.list_2_link(li3)
    link4 = DataTransformat.list_2_link(li4)

    with exec_time():
        print(Solution.isPalindrome2(link1))
        print(Solution.isPalindrome2(link2))
        print(Solution.isPalindrome2(link3))
        print(Solution.isPalindrome2(link4))
def get_swap_pairs():
    li1 = [1, 2, 3, 4, 5, 6]
    li2 = [3, 4, 5, 6, 7]

    link1 = DataTransformat.list_2_link(li1)
    link2 = DataTransformat.list_2_link(li2)

    with exec_time():
        result_link1 = Solution().swapPairs(link1)
    with exec_time():
        result_link2 = Solution().swapPairs(link2)

    result_list1 = DataTransformat.link_2_list(result_link1)
    result_list2 = DataTransformat.link_2_list(result_link2)

    print(result_list1)
    print(result_list2)
Exemplo n.º 11
0
def delete_node():
    link_str = input("Please input value(split with space):")
    del_node = input("Please input delete value:")

    origin_list = link_str.strip().split(' ')
    origin_link = DataTransformat.list_2_link(origin_list)

    delete_value = del_node.strip().split(' ')

    if (len(delete_value) == 1 and delete_value[0] in origin_list
            and delete_value[0] != origin_list[-1]):
        delete_node = DataTransformat.find_node(origin_link, delete_value[0])
        Solution.delete_node(delete_node)
    else:
        print("您输入的删除值,不在链表内,或在链表最后一项,或链表内有重复的元素")

    res = DataTransformat.link_2_list(origin_link)
    print(res)
Exemplo n.º 12
0
def test_reverseKGroup():
    li = [1, 2, 3, 4, 5]
    link = DataTransformat.list_2_link(li)
    res = Solution().reverseKGroup(link, 2)
    print(DataTransformat.link_2_list(res))

    li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    link = DataTransformat.list_2_link(li)
    res = Solution().reverseKGroup(link, 3)
    print(DataTransformat.link_2_list(res))

    li = [1]
    link = DataTransformat.list_2_link(li)
    res = Solution().reverseKGroup(link, 3)
    print(DataTransformat.link_2_list(res))
Exemplo n.º 13
0
def get_numComponents():
    print(Solution().numComponents(DataTransformat.list_2_link([0, 1, 2, 3]),
                                   [0, 1, 3]))
Exemplo n.º 14
0
def test_insertionSortList():
    print(DataTransformat.link_2_list(Solution().insertionSortList(DataTransformat.list_2_link([1, 3, 5, 2, 4]))))
    print(DataTransformat.link_2_list(Solution().insertionSortList(DataTransformat.list_2_link([2, 9, 3, 1, 7]))))
    print(DataTransformat.link_2_list(Solution().insertionSortList(DataTransformat.list_2_link([2, 3, 3, 1, 2]))))
    print(DataTransformat.link_2_list(Solution().insertionSortList(DataTransformat.list_2_link([123, 3, 2, 1, -2]))))
Exemplo n.º 15
0
def delete_more_duplicate():
    li = [1, 2, 3, 3, 3, 4, 4, 4, 5, 6]
    link = DataTransformat.list_2_link(li)
    res_link = Solution().deleteMoreDuplicates(link)
    res_list = DataTransformat.link_2_list(res_link)
    print(res_list)
def delete_duplicate():
    li = [1, 3, 3, 6, 7, 8, 8, 10, 11, 11]
    link = DataTransformat.list_2_link(li)
    res_link = Solution().deleteDuplicates(link)
    res_list = DataTransformat.link_2_list(res_link)
    print(res_list)
Exemplo n.º 17
0
def split_list_to_parts(test_li, test_part):
    test_link = DataTransformat.list_2_link(test_li)
    result = Solution().splitListToParts(test_link, test_part)
    for i in result:
        print(DataTransformat.link_2_list(i))
Exemplo n.º 18
0
def test_sortList():
    print(DataTransformat.link_2_list(Solution().sortList(DataTransformat.list_2_link([4, 2, 1, 3]))))
    print(DataTransformat.link_2_list(Solution().sortList(DataTransformat.list_2_link([-1, 5, 3, 4, 0]))))
Exemplo n.º 19
0
def test_removeNthFromEnd():
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1, 2, 3, 4, 5]), 1)))
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1, 2, 3, 4, 5]), 2)))
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1, 2, 3, 4, 5]), 3)))
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1, 2, 3, 4, 5]), 4)))
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1, 2, 3, 4, 5]), 5)))
    print(DataTransformat.link_2_list(Solution.removeNthFromEnd(DataTransformat.list_2_link([1]), 1)))