How to break the two layers of paper that python programmed
# Coding:utf-8
"""
How to break the two layers of paper that python programmed
Posted on someone asked
Define a function, named Samesums (alist), alist is a shape list, function is to judge can be divided into two groups, so that two sets of numbers and equal. If the return value is true, the return value is False if it is not possible. The following example:
Samesums ([4, 7, 6, 3])--True//4+6 = ten and 7 + 3 = 10
Samesums ([3, 3])--True
Samesums ([4, +])--True//4+12= and 16
Samesums ([5, 1])--False
This topic, for beginners, a bit difficult, but a little bit algorithmic basis, programming ideas, it is not difficult.
First of all, a story: two children from the tree to pick up the chestnut, the final merger of a pile of chestnut, the acquisition of a candidate for a point.
Assuming that human nature is greedy, the first person to choose First, choose the largest, the second choice of people, choose the big, has been circulating.
Greedy algorithm (also known as greedy algorithm) refers to, in the problem solving, always make the best choice at present.
In other words, not considering the overall optimality, he makes a local optimal solution in a sense.
This topic: First list from the big to the small sort, the middle set 2 empty list, from the big start Select, the next time you choose, you need
Compare and, if who's and small, add another one until the last element.
This article was trained by Huanggo Python, written by Huanggo
Huanggo Python training preview video playback address
Https://github.com/pythonpeixun/article/blob/master/python_shiping.md
"""
def samesums (int_list):
"" "Huanggo python training Huanggo wrote qq:1465376564
>>> Samesums ([4, 7, 6, 3])
True
>>> Samesums ([3, 3])
True
>>> Samesums ([4, 12, 16])
True
>>> Samesums ([5, 1])
False
"""
New_lst = sorted (int_list, Reverse=true)
List1 = List ()
List2 = List ()
For N in New_lst:
If sum (List1) < sum (LIST2):
List1.append (N)
Else
List2.append (N)
return sum (list1) = = SUM (list2)
if __name__ = = "__main__":
Import Doctest
Doctest.testmod ()
LST = [3, 9, 10, 30, 8]
Print Samesums (LST)
How to break the two layers of paper that python programmed