class Array:
def __init__(self, elements):
self.data = elements
def access(self, index):
# Return the value at the given index
return self.data[index]
def update(self, index, value):
# Change the value at the given index
self.data[index] = value
def __str__(self):
# Format array for printing
return ' | '.join(f'[{i}] -> {v}' for i, v in enumerate(self.data))
def main():
arr = Array([5, 10, 15, 20, 25])
print('Original array:')
print(arr)
# Access element at index 2
val = arr.access(2)
print(f'Value at index 2 before update: {val}')
# Update element at index 2 to 99
arr.update(2, 99)
print('Array after update at index 2:')
print(arr)
if __name__ == '__main__':
main()Access the value at the given index to read it
Update the value at the given index to the new value
Original array:
[0] -> 5 | [1] -> 10 | [2] -> 15 | [3] -> 20 | [4] -> 25
Value at index 2 before update: 15
Array after update at index 2:
[0] -> 5 | [1] -> 10 | [2] -> 99 | [3] -> 20 | [4] -> 25