Weekly Algorithmic Programming Contact 1

Source: Internet
Author: User

In an ordered list, find the index position of the specified number:

MyList = [1,2,3,4,5,6,7,8,9,10,11]

# List_len = mylist.__len__ ()
# Print List_len


def find_number_index (List,target,start):
Length = list.__len__ ()
Print "---" +str (length)
mid = Int (LENGTH/2)

If Mid <=1:
If list[mid] = = target:
Return "Find index:" + str (mid+ start)
Elif List[mid-1] = = target:
Return "Find index:" + str (mid-1+ start)
Elif length = = 3 and list[mid+1] = = target:
Return "Find index:" + str (mid + 1 + start)
Else
Return "could not mad a number"
Elif List[mid] = = target:
Print "mylist[mid] = = Target"
Return "Find index:" + str (mid + start)
Elif List[mid] > target:
Print str (list[mid]) + "Mylist[mid" > Target "
List_min = List[:mid]
Print List_min
Return Find_number_index (List_min,target,start)
else:
Print "Mylist[mid] < target"
List_max = List[mid:]
Print List_max
Return Find_number_index (List_max,target, Start+mid)


If __name__== "__main__":
Print Find_number_index (mylist,3, 0)

Weekly Algorithmic Programming Contact 1

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.