正在加载图片...
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Strategy 1:Linear Search The natural way to find (or not find)a number in a list is to start at the top of the list,scanning every element and comparing it with the number.Either you find it or you reach the end of the list allows you to answer. This strategy is called a linear search,where you search through the list of items one by one until the target value is found. def search(x,nums): for i in range(len (nums)): if nums[i]==x:item found,return the index value return i return -1 loop finished,item was not in list This algorithm wasn't hard to develop,and works well for modest-sized lists. 1010 Strategy 1: Linear Search • The natural way to find (or not find) a number in a list is to start at the top of the list, scanning every element and comparing it with the number. Either you find it or you reach the end of the list allows you to answer. • This strategy is called a linear search, where you search through the list of items one by one until the target value is found. • def search(x, nums): for i in range(len(nums)): if nums[i] == x: # item found, return the index value return i return -1 # loop finished, item was not in list • This algorithm wasn’t hard to develop, and works well for modest-sized lists
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有