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 diff --git a/lecture_2/HW1.ipynb b/lecture_2/HW1.ipynb new file mode 100644 index 0000000..50dd8bd --- /dev/null +++ b/lecture_2/HW1.ipynb @@ -0,0 +1,406 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "from array import array\n", + "\n", + "class arraylist:\n", + " \n", + " def __init__(self,typecode,data):\n", + " self.capacity = len(data)\n", + " self.typecode = typecode\n", + " self.arraylist = array(typecode, data)\n", + " \n", + " def __len__(self):\n", + " return len(self.arraylist)\n", + "\n", + " def __contains__(self, item):\n", + " if type(item) == self.typecode:\n", + " for x in self:\n", + " if item == x:\n", + " return True\n", + " return False\n", + " \n", + " def __getitem__(self,index):\n", + " return self.arraylist[index]\n", + " \n", + " def __delitem__(self,index):\n", + " t = self.arraylist[:index]\n", + " t = t + self.arraylist[index + 1:]\n", + " self.arraylist = array(self.typecode,t)\n", + " self.capacity -= 1\n", + " return self\n", + " \n", + " def __iadd__(self, data):\n", + " if ('len' in dir(data)) or ('__len__' in dir(data)):\n", + " amount = len(data)\n", + " self.capacity += amount\n", + " for i in range (amount):\n", + " self.arraylist+=array(self.typecode,[data[i]])\n", + " else:\n", + " self.arraylist=array(self.typecode,self.arraylist[:]+array(self.typecode,[data]))\n", + " self.capacity+=1\n", + " return self\n", + " \n", + " def __setitem__(self, index, value):\n", + " t = self.arraylist[:index]\n", + " v = array(self.typecode,[value])\n", + " t = t + v + self.arraylist[index + 1:]\n", + " self.arraylist = array(self.typecode,t)\n", + " return self\n", + " \n", + " def __iter__(self):\n", + " for i in range(len(self)):\n", + " yield self.arraylist[i]\n", + " \n", + " def __reversed__(self):\n", + " for i in range(len(self)-1,-1,-1):\n", + " yield self.arraylist[i]\n", + " \n", + " def index(self, item):\n", + " if type(item) == self.typecode:\n", + " for i, x in enumerate(self):\n", + " if x == item:\n", + " return i\n", + " return None\n", + "\n", + " def count(self, item):\n", + " i = 0\n", + " if self.typecode == type(item):\n", + " for x in self:\n", + " if x == item:\n", + " i += 1\n", + " return i\n", + " else:\n", + " return None\n", + " \n", + " def remove(self, item):\n", + " a = self.arraylist.index(item)\n", + " if a:\n", + " t = self.arraylist[:a]\n", + " t = t + self.arraylist[a + 1:]\n", + " self.arraylist = array(self.typecode,t)\n", + " self.capacity -= 1\n", + " \n", + " def pop(self, index):\n", + " x = self[index]; \n", + " self.remove(self[index]); \n", + " self.capacity -= 1\n", + " return x\n", + " \n", + " def append(self,data):\n", + " if (('len' in dir(data)) or ('__len__' in dir(data))):\n", + " amount = len(data)\n", + " self.capacity += amount\n", + " for i in range (amount):\n", + " self.arraylist+=array(self.typecode,[data[i]])\n", + " else:\n", + " print('You were visited by code police. No fine this time, use function \"extend\" henceforth :)')\n", + " self.arraylist=array(self.typecode,self.arraylist[:]+array(self.typecode,[data]))\n", + " self.capacity+=1\n", + " \n", + " def insert(self, index, item):\n", + " if index!=0:\n", + " t = self.arraylist[index:]\n", + " t = self.arraylist[:index] + array(self.typecode,[item]) + t\n", + " elif index == 0:\n", + " t = array(self.typecode,[item]) + self.arraylist[index:]\n", + " self.arraylist = array(self.typecode,t)\n", + " self.capacity += 1\n", + " \n", + " def extend(self, item):\n", + " self.arraylist=array(self.typecode,self.arraylist[:]+array(self.typecode,[item]))\n", + " self.capacity+=1" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "a = arraylist('i', [1,2,3,4,5])\n", + "for i in reversed(a):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [1, 2, 3, 3, 4, 5])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.insert(3, 3)\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 1, 2, 3, 3, 4, 5])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.insert(0, 0)\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 1, 2, 3, 3, 4, 5, 6, 7, 8])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.append([6,7,8])\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 1, 2, 3, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.extend(9)\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 1, 2, 3, 3, 4, 5, 6, 7, 8])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.remove(9)\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "print(a.arraylist.count(3))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n" + ] + }, + { + "data": { + "text/plain": [ + "array('i', [0, 1, 2, 3, 3, 4, 5, 6, 7])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "print(a.pop(9))\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 11, 2, 3, 3, 4, 5, 6, 7])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a[1] = 11\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 11, 2, 3, 3, 4, 5, 6, 7, 8])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a += 8\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array('i', [0, 2, 3, 3, 4, 5, 6, 7, 8])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del a[1]\n", + "a.arraylist" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}