general: ex2: add solution

This commit is contained in:
Bruno BELANYI 2022-11-21 16:11:45 +01:00
parent d535ee6e3f
commit e2a6b24076
1 changed files with 40 additions and 0 deletions

40
general/ex2.py Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
def skip_first(arr: list[int]) -> list[int]:
if len(arr) <= 1:
return []
first = arr[0]
for i in range(1, len(arr)):
if first != arr[i]:
break
# The loop must have iterated at least once, i is bound
return arr[i:] # type: ignore
def solution(arr: list[int], other: list[int]) -> list[int]:
res = []
while True:
if not arr or not other:
break
a = arr[0]
b = other[0]
if a == b:
res.append(a)
if a <= b:
arr = skip_first(arr)
if b <= a:
other = skip_first(other)
return res
def pythonic_solution(arr: list[int], other: list[int]) -> list[int]:
return list(set(arr) & set(other))
if __name__ == "__main__":
arr1 = [0, 1, 2, 3, 4, 5, 5, 5, 6, 7]
arr2 = [0, 0, 3, 3, 3, 5, 6, 6, 7, 8]
print(solution(arr1, arr2))
print(pythonic_solution(arr1, arr2))