diff --git a/lecture_2/Lecture 2.ipynb b/lecture_2/Lecture 2.ipynb index 7cf5668..110aed0 100644 --- a/lecture_2/Lecture 2.ipynb +++ b/lecture_2/Lecture 2.ipynb @@ -1874,7 +1874,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -1904,7 +1904,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -1949,7 +1949,7 @@ }, { "cell_type": "code", - "execution_count": 160, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -1959,7 +1959,7 @@ }, { "cell_type": "code", - "execution_count": 166, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -1979,7 +1979,7 @@ }, { "cell_type": "code", - "execution_count": 167, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -1988,21 +1988,26 @@ }, { "cell_type": "code", - "execution_count": 168, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "52\n", + "100\n", "29\n", - "44\n", - "28\n", + "73\n", "41\n", - "39\n", - "91\n", - "37\n" + "44\n", + "51\n", + "46\n", + "61\n", + "71\n", + "9\n", + "89\n", + "58\n", + "50\n" ] } ], @@ -2014,16 +2019,16 @@ }, { "cell_type": "code", - "execution_count": 169, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "52" + "100" ] }, - "execution_count": 169, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -2035,16 +2040,16 @@ }, { "cell_type": "code", - "execution_count": 170, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "8" + "13" ] }, - "execution_count": 170, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -2086,6 +2091,196 @@ "Hint 2 - наследование от классов из ```collections.abc``` **не лопускается!**" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "len = 4\n", + "1st element = 0\n", + "slice 1:3:1 = [0.4, 0.21]\n", + "contains 0.400000 is True \n", + "contains 0.100000 is False \n", + " at 0x776b373f20>\n", + "0.3\n", + "0.4\n", + "0.21\n", + "0.4\n", + "0.3\n", + "0.4\n", + "0.21\n", + "0.4\n", + "index of 0.400000 = 1\n", + "array after reverse [0.4, 0.21, 0.4, 0.3]\n", + "index of 0.400000 = 0\n", + "counts of 0.500000 = 0\n", + "counts of 0.400000 = 2\n", + "counts of 0.300000 = 1\n", + "After set [0.4, 0.21, 0.14, 0.3]\n", + "Deletes!\n", + "After deletes [0.4, 0.14, 0.3]\n", + "After implement append to (0.68,) result is [0.4, 0.14, 0.3, 0.68]\n", + "After implement insert to (1, 0.56) result is [0.4, 0.56, 0.14, 0.3, 0.68]\n", + "After inserts 0.560000 to 1 [0.4, 0.56, 0.14, 0.3, 0.68]\n", + "After self reverse by reverse() [0.68, 0.3, 0.14, 0.56, 0.4]\n", + "After implement extend to ([3.5, 0.8],) result is [0.68, 0.3, 0.14, 0.56, 0.4, 3.5, 0.8]\n", + "Pop result 0.8 and array [0.68, 0.3, 0.14, 0.56, 0.4, 3.5]\n", + "Pop result 3.5 and array [0.68, 0.3, 0.14, 0.56, 0.4]\n", + "remove 0.300000 result array [0.68, 0.14, 0.56, 0.4]\n", + "iadd result [2.4, 6.9] and array [0.68, 0.14, 0.56, 0.4, 2.4, 6.9]\n" + ] + } + ], + "source": [ + "from array import array\n", + "from math import floor\n", + "\n", + "class ArrayList:\n", + " def __init__(self, char, collection):\n", + " self.__char = char\n", + " self.__inner_array = array(char,collection)\n", + " self.__list_counter = 0\n", + " \n", + " def __len__(self):\n", + " counts = 0\n", + " for elem in self.__inner_array:\n", + " counts+=1\n", + " return counts\n", + " \n", + " def __getitem__(self,index):\n", + " if type(index) == slice:\n", + " getitem_list = []\n", + " for elem in self.__inner_array[index]:\n", + " getitem_list+=[elem]\n", + " return getitem_list\n", + " else:\n", + " return self.__inner_array[index]\n", + " \n", + " def __setitem__(self,index,value):\n", + " self.__inner_array[index] = value\n", + " \n", + " def __delitem__(self, key):\n", + " print(\"Deletes!\")\n", + " del self.__inner_array[key]\n", + " \n", + " def __contains__(self,item):\n", + " return (item in self.__inner_array)\n", + " \n", + " def __iter__(self):\n", + " return self\n", + " \n", + " def __next__(self):\n", + " if self.__list_counter < self.__len__():\n", + " self.__list_counter+=1\n", + " return self.__inner_array[self.__list_counter-1]\n", + " else:\n", + " self.__list_counter = 0\n", + " raise StopIteration\n", + " \n", + " def __reversed__(self):\n", + " len = self.__len__()\n", + " for i in range(floor(len/2)):\n", + " elem = self.__inner_array[i]\n", + " self.__inner_array[i] = self.__inner_array[len - i - 1]\n", + " self.__inner_array[len - i - 1] = elem\n", + " \n", + " def insert(self,index,item):\n", + " innerarray = self.__inner_array[0:index]\n", + " innerarray+= array(self.__char,[item])\n", + " innerarray+= self.__inner_array[index::]\n", + " del self.__inner_array\n", + " self.__inner_array = innerarray\n", + " \n", + " def index(self,item):\n", + " index = 0\n", + " for elem in self.__inner_array:\n", + " if elem == item:\n", + " return index\n", + " index+=1\n", + " return 0\n", + " \n", + " def count(self,item):\n", + " counts = 0\n", + " for elem in self.__inner_array:\n", + " if elem == item:\n", + " counts+=1\n", + " return counts\n", + " \n", + " def append(self,item):\n", + " self.__inner_array+=array(self.__char,[item])\n", + " \n", + " def reverse(self):\n", + " self.__reversed__()\n", + " \n", + " def extend(self,item):\n", + " self.__inner_array+=array(self.__char,item)\n", + " \n", + " def pop(self):\n", + " number_be_dead = len(self)-1\n", + " return_item = self.__inner_array[number_be_dead]\n", + " del self.__inner_array[number_be_dead]\n", + " return return_item\n", + " \n", + " def remove(self, value):\n", + " for i in range(len(self.__inner_array)):\n", + " if self.__inner_array[i] == value:\n", + " del self.__inner_array[i]\n", + " break\n", + "\n", + " def __iadd__(self,value):\n", + " self.__inner_array+=array(self.__char,value)\n", + " return self\n", + " \n", + " def return_it(self):\n", + " return list(self.__inner_array)\n", + "\n", + "this_arraylist = ArrayList(\"d\",[0.3,0.4,0.21,0.4])\n", + "print(\"len = %d\" %len(this_arraylist))\n", + "print(\"1st element = %d\" %this_arraylist[1])\n", + "print(\"slice 1:3:1 = %s\" %str(this_arraylist[1:3:1]))\n", + "print(\"contains %f is %s \" % (0.4, str(0.4 in this_arraylist)))\n", + "print(\"contains %f is %s \" % (0.1, str(0.1 in this_arraylist)))\n", + "print(gnome for gnome in this_arraylist)\n", + "for gnome in this_arraylist:\n", + " print(gnome)\n", + "for gnome in this_arraylist:\n", + " print(gnome)\n", + "print(\"index of %f = %s\" % (0.4, str(this_arraylist.index(0.4))))\n", + "reversed(this_arraylist)\n", + "print(\"array after reverse %s\" % str(this_arraylist.return_it()))\n", + "print(\"index of %f = %s\" % (0.4, str(this_arraylist.index(0.4))))\n", + "print(\"counts of %f = %d\" %(0.5, this_arraylist.count(0.5)))\n", + "print(\"counts of %f = %d\" %(0.4, this_arraylist.count(0.4)))\n", + "print(\"counts of %f = %d\" %(0.3, this_arraylist.count(0.3)))\n", + "this_arraylist[2] = 0.14\n", + "print(\"After set %s\" %str(this_arraylist.return_it()))\n", + "del this_arraylist[1]\n", + "print(\"After deletes %s\" %str(this_arraylist.return_it()))\n", + "def test_method(arraylist,method_to_do,*value_to_test):\n", + " method_to_do(*value_to_test)\n", + " print(\"After implement %s to %s result is %s\" %(str(method_to_do.__name__),str(value_to_test),str(arraylist.return_it())))\n", + "test_method(this_arraylist,this_arraylist.append,0.68)\n", + "test_method(this_arraylist,this_arraylist.insert,1,0.56)\n", + "print(\"After inserts %f to %d %s\" %(0.56,1.0,str(this_arraylist.return_it())))\n", + "this_arraylist.reverse()\n", + "print(\"After self reverse by reverse() %s\" %str(this_arraylist.return_it()))\n", + "list_to_extend = [3.5,0.8]\n", + "test_method(this_arraylist,this_arraylist.extend,list_to_extend)\n", + "#test_method([\"п\",\"о\",\"ф\",\"1\",\"g\"],this_arraylist.extend)\n", + "#test_method(5,this_arraylist,this_arraylist.extend)\n", + "print(\"Pop result %s and array %s\" %(str(this_arraylist.pop()),str(this_arraylist.return_it())))\n", + "print(\"Pop result %s and array %s\" %(str(this_arraylist.pop()),str(this_arraylist.return_it())))\n", + "this_arraylist.remove(0.3)\n", + "print(\"remove %f result array %s\" %(0.3,str(this_arraylist.return_it())))\n", + "list_to_test=[2.4,6.9]\n", + "this_arraylist+=list_to_test\n", + "print(\"iadd result %s and array %s\" %(str(list_to_test),str(this_arraylist.return_it())))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -2135,7 +2330,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.6.3" }, "toc": { "base_numbering": 1,