Python面試題,排列生成的解法,最優解答

 

有兩個序列a,b, 大小都爲n, 序列元素的值爲任意整型數,無序;

 

要求:通過交換a,b中的[序列a元素的和]與[序列b元素的和]之間的差最小。

 

我的解答:

最後的解答結果都是我見到的測試方法,此方法本質上是窮舉法,必然是最優解答。

 

在遞歸函數裏保存列表is_used(用來保存某個元素是否已經用過了)的時候,一定要注意用copy.deepcopy.

不然保存的所有列表都是[0,0,0,.....], 因爲如果不用前敘的方法的話, append()的是所有list的引用,而所有的這些引用指向的是同一個list對象,所以所有的子序列都一樣,而這個序列最後返回到了初試狀態,都是0,切記切記!

 

 

# it can be seen as  a computation problem,
# so use un-repeatible enumerate method to find the best result


import copy


# test suit
# au: thomas will lee, lzj, 

# new = [1, 2, 3, 4, 5, 6, 700, 800]  # my 97 91, better than 99

new = [93, 91, 90, 82, 81, 74, 74, 74, 74, 68]

# store_dict = {min_difference: []}

# new = [1, 50, 90, 100, 10000, 10001]  # my 38, 38, worth than 40


# new = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # 1 , 1 ;1



# new = [1, 1, 2, 3, 29, 30, 210, 232, 12311, 12312] # abs(-19)= 19 ,17, better than 21
# min_custom_enumerate(new)


sub_len = len(new) / 2
# to enumerate numbers in the list new
# a_iter = [0 for i in xrange(sub_len)]
# record whether one element is used
is_used = [0 for i in xrange(len(new))]

median = len(new) / 2
    # use a list as a stack to store all scenarios
    # sf = open('store_file','w') 
    # min_custom_enumerate_recursive(0)

all_list = []

def min_custom_enumerate_recursive(k):
    if k == sub_len:
        all_list.append(copy.deepcopy(is_used)) 
        # min_recode()
        # all_list.append(a_iter)
        # sf.write(str(is_used))
        # all_list += is_used
    else:
        for i in xrange(len(new)):
            if not is_used[i]:
                # all_list.append(is_used)
                # a_iter[k] = new[i]
                is_used[i] = 1  # used
                min_custom_enumerate_recursive(k + 1)
                is_used[i] = 0  # after the recursion backtracks


def solve():    
    # len(new) is always even
    min_custom_enumerate_recursive(0)    
        # with open("store_file") as ff:
    # # c = 1
    #      str(ff.readlines())

    # print all_list
    min_difference = abs(sum(new[:median]) - sum(new[median:]))
    # min_difference = 1000000000000
    all_list_len = len(all_list)
    final_a = []
    final_b = []
    for line in all_list:
        line_a = [new[i] for i in xrange(len(line)) if line[i] == 1]
        line_b = [new[i] for i in xrange(len(line)) if line[i] == 0]
        # print 'line_a',line_a
        # print 'line_b',line_b

        # print line_b
        line_a_sum = sum(line_a)
        line_b_sum = sum(line_b)
        new_difference = abs(line_a_sum - line_b_sum)
        if new_difference < min_difference:
            min_difference = new_difference
            final_a = copy.deepcopy(line_a)
            final_b = copy.deepcopy(line_b)
    print 'smallest value is', min_difference
    print 'a', final_a
    print 'b', final_b

solve()

 

 

 

 

 

發佈了40 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章