#Type of algorithm - inserting new element to pre-existing listfruit_name = ["Jackfruit","Honeydew","Grapes"]user_input1 =input("Please enter a fruit name: ")fruit_name.append(user_input1)print('The updated list is: '+str(fruit_name))#Type of algorithm - deleting element from listuser_input2 =input("Please enter the name of the fruit youwant to delete: ")fruit_name.remove(user_input2)print('The updated list is: '+str(fruit_name))
Please enter a fruit name: AppleThe updated listis: ['Jackfruit','Honeydew','Grapes','Apple']Please enter the name of the fruit you want to delete: AppleThe updated listis: ['Jackfruit','Honeydew','Grapes']Process finished withexit code 0
显示算法运行所用的时间:
# a is a list containing some numbers#We will compare the number input by user with the numbers in# this listimport timeittic=timeit.default_timer()a=[1,2,3,4,5,6,7,8]INPUT =input("Please input a number of your choice: ")number =int(INPUT)for i inrange(len(a)):if a[i]== number:print("Yes", end=' ')else:print("No", end=' ')print()toc=timeit.default_timer()time_elapsed = toc - ticprint("The time elapsed for this computation is: "+str(time_ elapsed) +"seconds")
Please input a number of your choice:1Yes No No No No No No NoThe time elapsed for this computation is:2.3035541 secondsProcess finished withexit code 0