最近在看《你也能看得懂的python算法书》,
自己来实现一下里面的算法吧。
有书里的有所不同。
比如这个题目,我的实现如下:
from django.test import TestCase import copy # Create your tests here. a_list = [3, 5, 8, 45, 78, 2345] b_list = [2, 5, 8, 23, 46, 89, 3235] a_len = 0 c_list = a_list.copy() for b_len in range(0, len(b_list)): while a_len < len(a_list): if b_list[b_len] <= a_list[a_len]: c_list.insert(a_len + b_len, b_list[b_len]) break else: a_len += 1 else: c_list = c_list + b_list[b_len:] break print(c_list) # ============分割线,上面是书上的,下面是自己的实现=================== a_len = 0 for b_item in b_list: while a_len < len(a_list): if b_item <= a_list[a_len]: a_list.insert(a_len, b_item) break a_len += 1 else: a_list.append(b_item) print(a_list)
输出:
[2, 3, 5, 5, 8, 8, 23, 45, 46, 78, 89, 2345, 3235] [2, 3, 5, 5, 8, 8, 23, 45, 46, 78, 89, 2345, 3235] Process finished with exit code 0