Skip to content

Instantly share code, notes, and snippets.

@odiak
Created July 7, 2020 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odiak/ef3de2b15eaa5e9a8ab416e20853e6be to your computer and use it in GitHub Desktop.
Save odiak/ef3de2b15eaa5e9a8ab416e20853e6be to your computer and use it in GitHub Desktop.
from typing import Tuple
import numpy as np
def n_union_and_intersection(a: np.ndarray, b: np.ndarray) -> Tuple[int, int]:
na = len(a)
nb = len(b)
ia = 0
ib = 0
if na == 0:
return (nb, 0)
if nb == 0:
return (na, 0)
cu = 2 if a[0] != b[0] else 1
ci = 0
ua = False
ub = False
while ia < na and ib < nb:
va = a[ia]
vb = b[ib]
eq = va == vb
lt = va < vb
gt = va > vb
ba = ia != na - 1
bb = ib != nb - 1
if eq:
if ua and ub:
cu += 1
ci += 1
else:
if ua:
cu += 1
if ub:
cu += 1
ua = False
ub = False
if eq:
if ba:
ua = True
if bb:
ub = True
elif lt and ba:
ua = True
elif gt and bb:
ub = True
elif ba:
ua = True
elif bb:
ub = True
if ua:
ia += 1
if ub:
ib += 1
if not ua and not ub:
break
return (cu, ci)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment