Python Learning--two numbers in a tuple combined with and equal to a certain number

Source: Internet
Author: User

#title: Finds the sum of two elements in an array equal to a given integer#Ideas:#1, the array elements are sorted;#2, Array[i] and A[j] (the value of J: i+1 to Len_array-1) add;#3, such as 22 add the integer continuation, such as = integer is the output element value;#4, if you exit directly, i+1 start the next round to add a comparisondefaddData (Array, sumdata): Temp_array=Array Temp_sumdata=SumdataPrint "Sumdata:%d"%Temp_sumdata#If there are no negative numbers in the array, you can first delete the number of >sumdata#To avoid the problem of length change, you can assign a number of 
    #Temp_array = []#len_temp_array2 = Len (temp_array2)#For i in range (0,len_temp_array2):#if Temp_array2[i] < temp_sumdata:#temp_array.append (Temp_array2[i])Sorted (temp_array) Len_temp_array=Len (Temp_array)#count the number of groups that meet the criterianum =0 forIinchRange (0, len_temp_array-1):         forJinchRange (i+1, Len_temp_array):ifTemp_array[i] + Temp_array[j] <Temp_sumdata:Continue            elifTemp_array[i] + temp_array[j] = =Temp_sumdata:num+ = 1Print "Group%d:"%NumPrint "Subscript :%d, element value:%d"%(i, temp_array[i])Print "Subscript :%d, element value:%d"%(J, Temp_array[j])Else:                 Break    if __name__=="__main__": Test_array= [ -1,1,2,3,4,5,6,7,8] Test_sumdata= 6AddData (Test_array, Test_sumdata)

Python Learning--two numbers in a tuple combined with and equal to a certain number

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.