Dynamic vector initialization
Create a function that initializes a dynamic one-dimensional list with a specified size. The function should return the initialized list. Use 0.0 as the initial value of each element.
Use a nonnegative integer size. The returned vector is a one-dimensional Python list. A size of 0 returns an empty list; each call creates a new list.
Complete the supplied function in Listing 1. Keep its signature and use the checks below it.
def initialize_dynamic_vector(size: int) -> list[float]:
# Add code here!
pass
def test_initialize_dynamic_vector() -> None:
size = 5
my_dynamic_list = initialize_dynamic_vector(size)
assert len(my_dynamic_list) == size
assert my_dynamic_list == [0.0, 0.0, 0.0, 0.0, 0.0]
print("Dynamic List Elements:")
print(my_dynamic_list)
test_initialize_dynamic_vector()
TipHint
len(values) returns the number of elements, so len([3, 7]) is 2 and len([]) is 0. List indexing starts at 0: [3, 7][0] is 3. An index must refer to an existing element.
assert condition stops the check with an AssertionError when the condition is false.