various fixes

This commit is contained in:
Cameron Neville
2017-09-16 14:15:31 -04:00
parent 6739e940c4
commit b37cf0ea94

View File

@@ -1,115 +1,125 @@
class sortedlist: class sortedlist:
#don't deal with this directly or you'll fuck everything up #don't deal with this directly or you'll fuck everything up
data = [] data = []
def __init__(self, dat=[], presorted=False): def __init__(self, dat=[], presorted=False):
if presorted: if presorted:
self.data = dat self.data = dat
else: else:
self.data = sorted(dat) self.data = sorted(dat)
def __getitem__(self, key): def __getitem__(self, key):
return self.data[key] return self.data[key]
def __len__(self): def __len__(self):
return len(self.data) return len(self.data)
def __str__(self): def __repr__(self):
return str(self.data) ret = 'sortedlist('
for i in self.data:
ret += str(i) + ', '
return ret[:-2] + ')'
def __delitem__(self, key): def __str__(self):
del(self.data[key]) return str(self.data)
def __add__(self, operand): def __delitem__(self, key):
if isinstance(operand, list): del(self.data[key])
return sortedlist(self.merge(self.data, sorted(operand)), True)
elif isinstance(operand, sortedlist):
return sortedlist(self.merge(self.data, operand.data), True)
else:
raise TypeError('can only concatenate list or sortedlist (not "%s") to sortedlist' % type(operand).__name__)
#because of sorting, addition is commutative (a + b == b + a) def __add__(self, operand):
def __radd__(self, operand): if isinstance(operand, list):
return self + operand return sortedlist(self.merge(self.data, sorted(operand)), presorted=True)
elif isinstance(operand, sortedlist):
return sortedlist(self.merge(self.data, operand.data), presorted=True)
else:
return NotImplemented
def __iadd__(self, operand): #because of sorting, addition is commutative (a + b == b + a)
self.data = (self + operand).data def __radd__(self, operand):
return self + operand
#takes two sorted lists (not sortedlists) and returns a sorted combination at O(n) def __iadd__(self, operand):
def merge(self, data1, data2): self.data = (self + operand).data
ind1 = 0 return self
ind2 = 0
merged = []
while True: #takes two sorted lists (not sortedlists) and returns a sorted combination at O(n)
if data1[ind1] <= data2[ind2]: def merge(self, data1, data2):
merged += [data1[ind1]] ind1 = 0
ind1 += 1 ind2 = 0
else: merged = []
merged += [data2[ind2]]
ind2 += 1 while True:
if ind1 >= len(data1): if data1[ind1] <= data2[ind2]:
return merged + data2[ind2:] merged += [data1[ind1]]
break ind1 += 1
elif ind2 >= len(data2): else:
return merged + data1[ind1:] merged += [data2[ind2]]
ind2 += 1
if ind1 >= len(data1):
return merged + data2[ind2:]
break
elif ind2 >= len(data2):
return merged + data1[ind1:]
def add(self, entry): def add(self, entry):
self.data = self.addhelper(self.data, entry) self.data = self.addhelper(self.data, entry)
#I couldn't figure out how to do this recursively in the add method #I couldn't figure out how to do this recursively in the add method
#and fuck trying to do this in a loop #and fuck trying to do this in a loop
def addhelper(self, dat, entry): def addhelper(self, dat, entry):
if dat == []: if dat == []:
return [entry] return [entry]
ind = int(len(dat)/2) ind = int(len(dat)/2)
if entry < dat[ind]: if entry < dat[ind]:
return self.addhelper(dat[0:ind], entry) + dat[ind:] return self.addhelper(dat[0:ind], entry) + dat[ind:]
else: else:
return dat[:ind+1] + self.addhelper(dat[ind+1:], entry) return dat[:ind+1] + self.addhelper(dat[ind+1:], entry)
def isin(self, entry): def isin(self, entry):
return self.inhelper(self.data, entry) return self.inhelper(self.data, entry)
#ditto #ditto
def inhelper(self, dat, entry): def inhelper(self, dat, entry):
if len(dat) == 0: if len(dat) == 0:
return False return False
elif len(dat) == 1: elif len(dat) == 1:
return entry == dat[0] return entry == dat[0]
ind = int(len(dat)/2) ind = int(len(dat)/2)
if entry < dat[ind]: if entry < dat[ind]:
return self.inhelper(dat[:ind], entry) return self.inhelper(dat[:ind], entry)
elif entry > dat[ind]: elif entry > dat[ind]:
return self.inhelper(dat[ind+1:], entry) return self.inhelper(dat[ind+1:], entry)
else: else:
return True return True
#returns an arbitrary index that contains the value at O(log(n)) #returns an arbitrary index that contains the value, at O(log(n))
#returns -1 if the list doesn't contain the value def index(self, value):
def index(self, val): low = 0
ind = self.indexhelper(self.data, val) high = len(self)
if ind == len(self): mid = (low + high) / 2
ind = -1 while self[mid] != value:
return ind if self[mid] > value:
pass high = mid
else:
low = mid
mid = (high + low) / 2
if high in (low, low+1):
raise ValueError('sortedlist does not contain entry')
return mid
#at this point I think I'm just bad at defining recursive functions #removes a single instance of entry, raises exception if it doesn't exist
def indexhelper(self, dat, val): def remove(self, entry):
ind = int(len(dat)/2) del(self[self.index(entry)])
if len(dat) == 0: #removes every instance of entry, doesn't raise an exception if it doesn't exist
return 0 #TODO: index once, then search adacent indices
elif dat[ind] == val: def removeall(self, entry):
return ind while True:
elif dat[ind] < val: try:
return ind + 1 + self.indexhelper(dat[ind+1:], val) del(self[self.index(entry)])
else: except ValueError:
return self.indexhelper(dat[:ind], val) break
#TODO: add functionality for single=False
def remove(self, entry, single=True):
del(self[self.index(entry)])