# 配列の最後の位置に要素を挿入する
def append(self, value: int):
# 配列が既に全て埋まっている場合は、サイズを拡張する
if self.length == self.capacity:
self.resize()
# 次の空いている位置に要素を挿入します
self.arr[self.length] = value
self.length += 1
def resize(self):
# サイズを2倍にした新しい配列を作成します
self.capacity = 2 * self.capacity
newArr = [0] * self.capacity
# 既存の全ての要素を新しい配列にコピーします
for i in range(self.length):
newArr[i] = self.arr[i]
self.arr = newArr
# リストの作成
array = [1, 2, 3, 4, 5]
# リストへの要素の追加 (O(1))
array.append(6) # array: [1, 2, 3, 4, 5, 6]
# リストの特定位置への要素の挿入 (O(n) - n is the number of elements to shift)
array.insert(0, 0) # array: [0, 1, 2, 3, 4, 5, 6]
# リストからの要素の削除 (O(n) - n is the number of elements to shift)
array.remove(3) # array: [0, 1, 2, 4, 5, 6]
# リストの特定のインデックスの要素の取得 (O(1))
print(array[2]) # Output: 2
# リストの長さの取得 (O(1))
print(len(array)) # Output: 6
# リストから最後の要素を削除 (O(1))
last_element = array.pop() # last_element: 6, array: [0, 1, 2, 4, 5]
# 文字列の作成
string = "Hello, World!"
# 文字列の連結 (O(n+m) - n and m are lengths of the strings)
string += " How are you?" # string: "Hello, World! How are you?"
# 文字列の特定のインデックスの文字の取得 (O(1))
print(string[0]) # Output: 'H'
# 文字列の長さの取得 (O(1))
print(len(string)) # Output: 29
# 文字列の分割 (O(n) - n is the number of characters in the string)
print(string.split(' ')) # Output: ['Hello,', 'World!', 'How', 'are', 'you?']