1) Write an expression that uses list indexing and list procedures. Write comments on your code to show what each line is doing. 2) Write a python function that can determine the worst case number of iterations of a binary search for an array of length 20.
# Create a list of flowers
flowers = ["Rose", "Tulip", "Lily", "Daisy", "Sunflower"]
# Access the first flower in the list using list indexing
first_flower = flowers[0]
print("The first flower is:", first_flower)
# Access the last flower in the list using negative list indexing
last_flower = flowers[-1]
print("The last flower is:", last_flower)
# Add a new flower to the end of the list using the append() method
flowers.append("Orchid")
print("Added Orchid to the list:", flowers)
# Insert a new flower at a specific position in the list using insert()
flowers.insert(2, "Carnation")
print("Inserted Carnation at index 2:", flowers)
# Remove a flower from the list by value using remove()
flowers.remove("Lily")
print("Removed Lily from the list:", flowers)
# Sort the list in alphabetical order using sort()
flowers.sort()
print("Sorted list of flowers:", flowers)
# Reverse the order of the list using reverse()
flowers.reverse()
print("Reversed list of flowers:", flowers)
The first flower is: Rose
The last flower is: Sunflower
Added Orchid to the list: ['Rose', 'Tulip', 'Lily', 'Daisy', 'Sunflower', 'Orchid']
Inserted Carnation at index 2: ['Rose', 'Tulip', 'Carnation', 'Lily', 'Daisy', 'Sunflower', 'Orchid']
Removed Lily from the list: ['Rose', 'Tulip', 'Carnation', 'Daisy', 'Sunflower', 'Orchid']
Sorted list of flowers: ['Carnation', 'Daisy', 'Orchid', 'Rose', 'Sunflower', 'Tulip']
Reversed list of flowers: ['Tulip', 'Sunflower', 'Rose', 'Orchid', 'Daisy', 'Carnation']
import math
def worst_case_binary_search_iterations(array_length):
iterations = math.ceil(math.log2(array_length))
return iterations
array_length = 30
print(worst_case_binary_search_iterations(array_length)) # Output the worst-case number of iterations
5
the answer to problem 3 is A