- Educative.io:
Python for Programmers
|Ace The Python Coding Interview
Command: python3 main.py -list
Command: python3 main.py -prob [PROB_ID: 0001, 0002, 0003, ...]
>>> from Array import Array
>>> import inspect
>>> code = inspect.getsource(Array.search("0027"))
>>> print(code)
@classmethod
def _0027_remove_element(self, nums: list[int], val: int) -> int:
""" Easy | Two Pointer """
# Time: O(n)
# Space: O(1)
l, r = 0, len(nums)-1
while r >= 0 and nums[r] == val: r -= 1 # skip duplicate
while l <= r:
if nums[l] == val:
nums[l], nums[r] = nums[r], nums[l]
r -= 1
while r >= 0 and nums[r] == val: r -= 1 # skip duplicate
else:
l += 1
return l
>>> from Test import Test
>>> Test.run("0027", nums=[3,2,2,3], val=3)
[2, 2, 3, 3]
>>> Test.run("0088", nums1=[1,2,3,0,0,0], m=3, nums2=[2,5,6], n=3)
[1, 2, 2, 3, 5, 6]