这是一个相当长但正确的(据我从问题讨论中得知)使用递归的实现。
要点:
- 我使用
.pop(index)
. 这让我可以使用递归,因为随着函数的递归,两个列表都变得越来越小,导致一个列表是len(0)
.
- 可以从任一列表中选择数字,并且可以从单个列表中连续选择的数字没有限制。
- 不允许连续重复。
- 比较两个不相等的数字时,较小的数字总是排在较大的位置。23xxx 总是低于 32xxx。
基本上,如果我有类似的东西[1,2,3]
then [6,0,4]
,第一个列表中的所有数字都将在第二个列表中的第一个数字之前,因为 1236xx总是小于 6xxxxx,1236xx 小于 16xxxx,1236xx 小于 126xxx,不管为 x 选择的值。
z = [None]
#set to None so that z[-1] doesn't throw an out-of-range error
def small_list(a,b): #recursive function
### BASE CASE ###
if len(a) == 0: #if one list empty, can only add rest of other list
for i in b:
if i != z[-1]: #account for duplicates
z.append(i)
# else: #don't add duplicates
return z.pop(0) #end recursion, remove extraneous None
elif len(b) == 0: #if one list empty, can only add rest of other list
for j in a:
if j != z[-1]:#account for duplicates
z.append(j)
# else: #don't add duplicates
return z.pop(0) #end recursion, remove extraneous None
#Otherwise, we need to check whichever is smaller.
#The smaller number should ALWAYS go in the larger place (tens,hundreds,etc.) to make a smaller number.
### RECURSIVE CASE ###
if a[0] < b[0]:
if a[0] != z[-1]:
z.append(a.pop(0))
else:
a.pop(0)
elif a[0] > b[0]:
if b[0] != z[-1]:
z.append(b.pop(0))
else:
b.pop(0)
elif a[0] == b[0]:
a.pop(0)
small_list(a,b) # recur
例子:
z = [None]
l1 = [1,2,3,2]
l2 = [2,1,1,1]
small_list(l1,l2)
print z
这第一个示例打印现在[1, 2, 1, 3, 2]
是正确的。
z = [None]
l1 = [1,2,3]
l2 = [4,5,6]
small_list(l1,l2)
print z
这第二个示例打印现在[1, 2, 3, 4, 5, 6]
也是正确的。
以下是它如何计算您上面给出的示例的流程:
# The format is: [final list] len(a) [list a] len(b) [list b]
[] len(a) = 6 [3, 4, 5, 7, 9, 2] len(b) = 6 [3, 5, 7, 4, 2, 8]
# 3 repeated, so remove it.
[] len(a) = 5 [4, 5, 7, 9, 2] len(b) = 6 [3, 5, 7, 4, 2, 8]
# add lower of first two indices to final (4 v 3), and remove from corresponding list
[3] len(a) = 5 [4, 5, 7, 9, 2] len(b) = 5 [5, 7, 4, 2, 8]
# add lower of first two indices to final (4 v 5), and remove from corresponding list
[3, 4] len(a) = 4 [5, 7, 9, 2] len(b) = 5 [5, 7, 4, 2, 8]
# 5 repeated, so remove it.
[3, 4] len(a) = 3 [7, 9, 2] len(b) = 5 [5, 7, 4, 2, 8]
# add lower of first two indices to final (7 v 5), and remove from corresponding list
[3, 4, 5] len(a) = 3 [7, 9, 2] len(b) = 4 [7, 4, 2, 8]
# 7 repeated, so remove it.
[3, 4, 5] len(a) = 2 [9, 2] len(b) = 4 [7, 4, 2, 8]
# add lower of first two indices to final (9 v 7), and remove from corresponding list
[3, 4, 5, 7] len(a) = 2 [9, 2] len(b) = 3 [4, 2, 8]
# add lower of first two indices to final (9 v 4), and remove from corresponding list
[3, 4, 5, 7, 4] len(a) = 2 [9, 2] len(b) = 2 [2, 8]
# add lower of first two indices to final (9 v 2), and remove from corresponding list
[3, 4, 5, 7, 4, 2] len(a) = 2 [9, 2] len(b) = 1 [8]
# add lower of first two indices to final (9 v 8), and remove from corresponding list
[3, 4, 5, 7, 4, 2, 8] len(a) = 2 [9, 2] len(b) = 0 []
# list b is empty, add first element of list a (if non-duplicate)
[3, 4, 5, 7, 4, 2, 8, 9] len(a) = 1 [2] len(b) = 0 []
# list b is empty, add first element of list a (if non-duplicate)
#Finally:
[3, 4, 5, 7, 4, 2, 8, 9, 2]