#Type of algorithm - inserting new element to pre-existing list
fruit_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 list
user_input2 = input("Please enter the name of the fruit you
want to delete: ")
fruit_name.remove(user_input2)
print('The updated list is: ' + str(fruit_name))
Please enter a fruit name: Apple
The updated list is: ['Jackfruit', 'Honeydew', 'Grapes',
'Apple']
Please enter the name of the fruit you want to delete: Apple
The updated list is: ['Jackfruit', 'Honeydew', 'Grapes']
Process finished with exit code 0
显示算法运行所用的时间:
# a is a list containing some numbers
#We will compare the number input by user with the numbers in
# this list
import timeit
tic=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 in range(len(a)):
if a[i]== number:
print("Yes", end=' ')
else:
print("No", end=' ')
print()
toc=timeit.default_timer()
time_elapsed = toc - tic
print("The time elapsed for this computation is: " + str(time_
elapsed) + "seconds")
Please input a number of your choice: 1
Yes No No No No No No No
The time elapsed for this computation is: 2.3035541 seconds
Process finished with exit code 0