Python version to find the maximum contiguous range of arrays

Source: Internet
Author: User

[This article is from the Sky Cloud-owned blog Park]

Title: There is an array that asks him for the maximum (longest) continuous interval (the number is a continuous interval).

My solution is as follows:

classFinder (object):" "determine whether two contiguous digits are contiguous, if continuous: 1. Continue to Judge 2. Record continuous length finally return maximum continuous length" "        defFind_continuity (self,index,array,length=1):        ifIndex+1 <Len (array): _curr=Array[index] _next= Array[index+1]            ifABS (_NEXT-_CURR) = = 1: Length+ = 1length= Self.find_continuity (index+1, Array,length)returnlength" "The index value at the beginning of each number and the maximum continuous interval length are stored in the dictionary to find the largest key value in the dictionary, that is, the maximum continuous interval returns the corresponding array slice based on the index and length of the maximum contiguous interval" "    deffind_longest (Self,array): Continuity=dict () forIinchRange (len (array)): Length=self.find_continuity (I,array) continuity[i]=length Longest= Max (Continuity.items (), key=LambdaX:x[1]) Index,length= Longest[0],longest[1]        returnarray[index:index+Length]if __name__=='__main__': Array= [1,2,3,4,5,4,2,5,3,4,7,1,5,9,10,9,8,7,6,5,4,3,4,5,6,8] Finder=Finder () Longest_array=finder.find_longest (Array)Print(Longest_array)

Python version to find the maximum contiguous range of arrays

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.