Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created June 9, 2017 22:18
Show Gist options
  • Save jordanlewis/0296ec2344b7d887716abc5bf849a78d to your computer and use it in GitHub Desktop.
Save jordanlewis/0296ec2344b7d887716abc5bf849a78d to your computer and use it in GitHub Desktop.
#!/usr/bin/python
class T(object):
def __init__(self, n=0, lists=None):
self.lists = lists if lists is not None else []
if lists is None:
# lists contains normal lists and t's as well
self.lists = []
# cached length prefix
self.n = n
# iterator position
self.i = 0
@staticmethod
def FromInt(n):
return T(n=n)
@staticmethod
def FromList(l):
return T(lists=[iter(l)])
def __iter__(self):
self.i = 0
return self
def next(self):
if self.i < self.n:
self.i += 1
return True
if len(self.lists) == 0:
raise StopIteration
for l in self.lists:
l.next()
self.n += 1
self.i += 1
return True
def min(self, t):
return T(lists=[iter(self), iter(t)])
def __cmp__(self, t):
l = iter(self)
r = iter(t)
rmore = lmore = True
while (lmore and rmore):
try:
l.next()
except StopIteration:
lmore = False
try:
r.next()
except StopIteration:
rmore = False
if not lmore and not rmore:
return 0
elif not lmore:
return -1
else:
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment