How to pierce the three layers of paper that python programmed

Source: Internet
Author: User

# Coding:utf-8

"""

How to pierce the three 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.

Before using greedy algorithm, can only meet the above 4 test cases of the correct solution.

How to break the two layers of paper that python programmed

Https://github.com/pythonpeixun/article/blob/master/pythonstudy2.md

But for the list of [4, 5, 6, 7, 8],[2, 2, 2, 3, 3], the correct answer cannot be answered.

This question: can use recursion to solve a problem, also can use the dynamic programming problem solving, Huanggo use such idea to solve for beginners reference!

Problem Solving Ideas:

1, if the sum (list) is an odd number, you cannot split the list into 2 and an equal list

2, if sum (list) is even, sub_sum = SUM (list)/2

2. List all possible subsets (sub list)

3, judge whether the subset is equal to sub_sum, if equal can be split into 2 and equal list,

Otherwise, you can't.

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

"""

Import Itertools

def get_all_subset (LST):

"" To list all child sets "" "

Tmp_lst = []

length = Len (LST)

For I in xrange (1, length):

Tmp_lst + = (set (itertools.combinations (LST, i)))

Return Tmp_lst

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

"""

Sum_of_lsit = SUM (int_list)

Sub_sum = SUM_OF_LSIT/2

If Sum_of_lsit% 2:

Return False

All_subset = Get_all_subset (int_list)

For item in All_subset:

If sum (item) = = Sub_sum:

Return True

Return False

if __name__ = = "__main__":

Import Doctest

Doctest.testmod ()

Lst1 = [3, 9, 10, 30, 8]

Lst2 = [4, 5, 6, 7, 8]

Lst3 = [2, 2, 2, 3, 3]

Print Samesums (lst1)

Print Samesums (LST2)

Print Samesums (LST3)


How to pierce the three layers of paper that python programmed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.