Implement class SortedSet on a plain Python list kept sorted with the bisect module:
add(x): insertx(no duplicates).floor(x): the largest stored value<= x, orNone.ceiling(x): the smallest stored value>= x, orNone.__len__(): how many values are stored.
s = SortedSet()
for v in [10, 3, 7, 3]:
s.add(v)
len(s) # 3 (stored: [3, 7, 10])
s.floor(8) # 7
s.ceiling(8) # 10
s.floor(7) # 7 (equal counts)
s.floor(2) # None (nothing is <= 2)
s.ceiling(11) # None
bisect.insort is fine for add. ⭐ Bonus: a speed test earns a star if floor and ceiling binary search (O(log n)).
Show hint
bisect_right(a, x) counts values <= x; bisect_left(a, x) counts values < x.