Python small algorithm (2), python Algorithm
There are two sequences a and B, all of which are n in size. The values of the sequence elements are unordered due to arbitrary integer numbers;
Requirement: by exchanging elements in a and B, the deviation between [Sum of Element a] and [Sum of Element B] is minimized. (Huawei interview)
Def diff (sorted_list, length): if not sorted_list: return ([], []) last = sorted_list [-1] big_list, small_list = diff (sorted_list [: -1], length) big_list_sum = sum (big_list) small_list_sum = sum (small_list) if big_list_sum> small_list_sum: if len (small_list) >=length: big_list.append (last) else: small_list.append (last) return (big_list, small_list) else: if len (big_list)> = length: small_list.append (last) else: big_list.append (last) return (small_list, big_list) a = [1, 3, 5, 7, 99] B = [2, 4, 6, 88, 10] c = [] c. extend (a) c. extend (B) p = sorted (c, reverse = True) w = diff (p, len (a) print w