Python uses the divide and conquer method to calculate the maximum value

Source: Internet
Author: User
This article mainly introduces python's method of using the divide and conquer method to calculate the maximum value. It gives a detailed analysis of the principle of the divide and conquer method and the method of realizing the maximum value, for more information, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

Question:

For a given sequence table, write a grouping Algorithm for Finding its maximum and minimum values.

Analysis:

As the structure of the sequence table is not given, here we use a simple sequence table to take an integer array. The size of the array is defined by the user and the data is randomly generated. We know that if the array size is 1, we can directly give the result. If the array size is 2, we can get the result after a comparison, so we find the sub-problem to solve this problem: array size <= 2. Now we can perform the partitioning operation. As long as the array length of the problem to be solved is greater than 2, we will continue to divide the problem. Otherwise, we will solve the subproblem and update the global solution. The following is the code.

The key is to break down the sequence table into a list with k elements of 2, find the maximum value of the list, and then merge the list of sub-problems, then recursively solve the problem.

On the code bar:

#-*-Coding: UTF-8-*-# division and Control Method for Solving the maximum value problem import random # Maximum Method for Solving the list of two elements def max_value (max_list): return max (max_list) # define the recursive method def solve (init_list): if len (init_list) <= 2: # if the number of list elements is less than or equal to 2, print max_value (init_list) else: init_list = [init_list [I: I + 2] for I in range (0, len (init_list), 2)] # split the list into the list length divided by two lists max_init_list = [] # list used to merge and calculate the maximum value in init_list: # merge the solving list of each subproblem into max_init_list.append (max_value (_ list) solve (max_init_list) if _ name _ = "_ main __": test_list = [,] # test list solve (test_list)

I hope this article will help you with Python programming.

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.