diff --git a/hw1/check.py b/hw1/check.py new file mode 100644 index 0000000..1316aa3 --- /dev/null +++ b/hw1/check.py @@ -0,0 +1,37 @@ +from hw1.source import arraylist + +if __name__ == "__main__": + a = arraylist('i', [1, 2, 3, 4, 5]) + for i in reversed(a): + print(i) + a.insert(3, 3) + print(a.arraylist) + + a.insert(0, 0) + print(a.arraylist) + + a.extend([6,7,8]) + print(a.arraylist) + + a.append(9) + print(a.arraylist) + + a.remove(9) + print(a.arraylist) + + print(a.arraylist.count(3)) + + print(a.pop(9)) + print(a.arraylist) + + a[1] = 11 + print(a.arraylist) + + a += 8 + print(a.arraylist) + + a+= [9,10] + print(a.arraylist) + + del a[1] + print(a.arraylist) diff --git a/hw1/source.py b/hw1/source.py new file mode 100644 index 0000000..b2dda71 --- /dev/null +++ b/hw1/source.py @@ -0,0 +1,146 @@ +import collections.abc +from array import array + + +class iterator: + + def __init__(self, list, direct): + self._list = list + self._direct = direct + if direct < 0: + self._ind = len(list)-1 + else: + self._ind = 0 + + def __iter__(self): + return self + + def __next__(self): + if self._ind < len(self._list) and self._ind >= 0: + x = self._list[self._ind] + self._ind += self._direct + return x + else: + raise StopIteration + + +class arraylist: + + def __init__(self, typ, data): + self.capacity = len(data) + self.typ = typ + self.arraylist = array(typ, data) + + def __len__(self): + return len(self.arraylist) + + def __contains__(self, item): + if type(item) == self.typ: + for x in self: + if item == x: + return True + return False + + def __getitem__(self, start, stop=None, step=1): + index = self.indcheck(start) + if stop == None: + end = start + 1 + else: + end = stop + return self.arraylist[index:end:step] + + def __delitem__(self, index): + x = self.indcheck(index) + t = self.arraylist[:x] + t = t + self.arraylist[x + 1:] + self.arraylist = array(self.typ, t) + self.capacity -= 1 + return self + + def __iadd__(self, data): + if isinstance(data, collections.abc.Sized): + amount = len(data) + self.capacity += amount + for i in range(amount): + self.arraylist += array(self.typ, [data[i]]) + else: + self.arraylist = array(self.typ, self.arraylist[:] + array(self.typ, [data])) + self.capacity += 1 + return self + + def __setitem__(self, index, value): + x = self.indcheck(index) + t = self.arraylist[:x] + v = array(self.typ, [value]) + t = t + v + self.arraylist[x + 1:] + self.arraylist = array(self.typ, t) + return self + + def __iter__(self): + return iterator(self.arraylist, 1) + + def __reversed__(self): + return iterator(self.arraylist, -1) + + def indcheck(self, index): + x = index + if index < 0: + x = self.capacity - (abs(index) % self.capacity) + elif index > self.capacity: + x = index % self.capacity + return x + + def index(self, item): + if type(item) == self.typ: + for i, x in enumerate(self): + if x == item: + return i + return None + + def count(self, item): + i = 0 + if self.typ == type(item): + for x in self: + if x == item: + i += 1 + return i + else: + return None + + def remove(self, item): + a = self.index(item) + if a: + t = self.arraylist[:a] + t = t + self.arraylist[a + 1:] + self.arraylist = array(self.typ, t) + self.capacity -= 1 + + def pop(self, index): + x = self[self.indcheck(index)] + self.remove(x) + self.capacity -= 1 + return x + + def append(self, item): + self.arraylist = array(self.typ, self.arraylist[:] + array(self.typ, [item])) + self.capacity += 1 + + def insert(self, i, item): + index = self.indcheck(i) + if index != 0: + t = self.arraylist[index:] + t = self.arraylist[:index] + array(self.typ, [item]) + t + elif index == 0: + t = array(self.typ, [item]) + self.arraylist[index:] + self.arraylist = array(self.typ, t) + self.capacity += 1 + + def extend(self, data): + if isinstance(data, collections.abc.Sized): + amount = len(data) + self.capacity += amount + for i in range(amount): + self.arraylist += array(self.typ, [data[i]]) + else: + self.arraylist = array(self.typ, self.arraylist[:] + array(self.typ, [data])) + self.capacity += 1