From 881b2c4b76846b0232ba29100ae9e6a741299f6d Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Tue, 24 Mar 2020 12:25:38 +0300 Subject: [PATCH 1/7] Homework 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Я попытался добить до mutable sequence, но не понял что делать в ее магических методах. --- lecture_2/HW1.ipynb | 290 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 lecture_2/HW1.ipynb diff --git a/lecture_2/HW1.ipynb b/lecture_2/HW1.ipynb new file mode 100644 index 0000000..e32737f --- /dev/null +++ b/lecture_2/HW1.ipynb @@ -0,0 +1,290 @@ +{ + "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 __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 = self.arraylist[a + 1:] + t\n", + " self.arraylist = array(self.typecode,t)\n", + " \n", + " def pop(self, index):\n", + " x = self[index]; \n", + " self.remove(self[index]); \n", + " return x\n", + " \n", + " def append(self,data):\n", + " amount = len(data)\n", + " self.capacity += amount\n", + " for i in range (amount):\n", + " self.arraylist+=array(self.typecode,[data[i]])\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.capacity+=1\n", + " self.arraylist=array(self.typecode,self.arraylist[:]+array(self.typecode,[item]))" + ] + }, + { + "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": 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 +} From 04668022e70e547e9550c8be916abd3aadb12a08 Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Sun, 29 Mar 2020 22:04:42 +0300 Subject: [PATCH 2/7] Delete HW1.ipynb --- lecture_2/HW1.ipynb | 290 -------------------------------------------- 1 file changed, 290 deletions(-) delete mode 100644 lecture_2/HW1.ipynb diff --git a/lecture_2/HW1.ipynb b/lecture_2/HW1.ipynb deleted file mode 100644 index e32737f..0000000 --- a/lecture_2/HW1.ipynb +++ /dev/null @@ -1,290 +0,0 @@ -{ - "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 __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 = self.arraylist[a + 1:] + t\n", - " self.arraylist = array(self.typecode,t)\n", - " \n", - " def pop(self, index):\n", - " x = self[index]; \n", - " self.remove(self[index]); \n", - " return x\n", - " \n", - " def append(self,data):\n", - " amount = len(data)\n", - " self.capacity += amount\n", - " for i in range (amount):\n", - " self.arraylist+=array(self.typecode,[data[i]])\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.capacity+=1\n", - " self.arraylist=array(self.typecode,self.arraylist[:]+array(self.typecode,[item]))" - ] - }, - { - "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": 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 -} From 9b6926ef310c850b28ba7a6073b5a30fa32c91b8 Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Sun, 29 Mar 2020 22:05:48 +0300 Subject: [PATCH 3/7] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D0=B0=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D0=B4=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lecture_2/HW1.ipynb | 406 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 406 insertions(+) create mode 100644 lecture_2/HW1.ipynb 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 +} From efe5cb18eb307973aab42f426f9930e5f4bb209b Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Mon, 30 Mar 2020 17:15:34 +0300 Subject: [PATCH 4/7] Create source.py --- hw1/source.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 hw1/source.py diff --git a/hw1/source.py b/hw1/source.py new file mode 100644 index 0000000..c2830ad --- /dev/null +++ b/hw1/source.py @@ -0,0 +1,142 @@ +import collections +from array import array + + +class iterator: + + def __init__(self, list, direct): + self._list = list + self._direct = direct + self._ind = 0 + + def __iter__(self): + return self + + def __next__(self): + if self._ind < len(self._list) and self._ind >= 0: + yield self._list[self._ind] + self._ind += self._direct + 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.__data[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 = 0 + 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.arraylist.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 From 6aec0e6909576eebcb1d96d48e4eb61544ac2adb Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Mon, 30 Mar 2020 17:41:54 +0300 Subject: [PATCH 5/7] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D0=BA=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D0=B5=D0=BC=D1=83=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw1/check.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 hw1/check.py 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) From 000bbd02829e7cca51bef1bb8cfaf666a7e24d18 Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Mon, 30 Mar 2020 17:44:14 +0300 Subject: [PATCH 6/7] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20=D0=B8=20=D0=B8=D1=82=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw1/source.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hw1/source.py b/hw1/source.py index c2830ad..b2dda71 100644 --- a/hw1/source.py +++ b/hw1/source.py @@ -1,4 +1,4 @@ -import collections +import collections.abc from array import array @@ -7,15 +7,19 @@ class iterator: def __init__(self, list, direct): self._list = list self._direct = direct - self._ind = 0 + 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: - yield self._list[self._ind] + x = self._list[self._ind] self._ind += self._direct + return x else: raise StopIteration @@ -43,7 +47,7 @@ def __getitem__(self, start, stop=None, step=1): end = start + 1 else: end = stop - return self.__data[index:end:step] + return self.arraylist[index:end:step] def __delitem__(self, index): x = self.indcheck(index) @@ -79,7 +83,7 @@ def __reversed__(self): return iterator(self.arraylist, -1) def indcheck(self, index): - x = 0 + x = index if index < 0: x = self.capacity - (abs(index) % self.capacity) elif index > self.capacity: @@ -104,7 +108,7 @@ def count(self, item): return None def remove(self, item): - a = self.arraylist.index(item) + a = self.index(item) if a: t = self.arraylist[:a] t = t + self.arraylist[a + 1:] From 2bf2c72b08855b5d8e4dff7510331382b517ece0 Mon Sep 17 00:00:00 2001 From: Alexanderpublic <51701242+Alexanderpublic@users.noreply.github.com> Date: Mon, 30 Mar 2020 17:51:04 +0300 Subject: [PATCH 7/7] Delete HW1.ipynb --- lecture_2/HW1.ipynb | 406 -------------------------------------------- 1 file changed, 406 deletions(-) delete mode 100644 lecture_2/HW1.ipynb diff --git a/lecture_2/HW1.ipynb b/lecture_2/HW1.ipynb deleted file mode 100644 index 50dd8bd..0000000 --- a/lecture_2/HW1.ipynb +++ /dev/null @@ -1,406 +0,0 @@ -{ - "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 -}