diff --git a/1anno/2trimestre/Coding for DataScience/Python/exercises_in_python_no_solution.ipynb b/1anno/2trimestre/Coding for DataScience/Python/exercises_in_python.ipynb similarity index 54% rename from 1anno/2trimestre/Coding for DataScience/Python/exercises_in_python_no_solution.ipynb rename to 1anno/2trimestre/Coding for DataScience/Python/exercises_in_python.ipynb index 827fbaddb..d27a2b1b0 100644 --- a/1anno/2trimestre/Coding for DataScience/Python/exercises_in_python_no_solution.ipynb +++ b/1anno/2trimestre/Coding for DataScience/Python/exercises_in_python.ipynb @@ -43,10 +43,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 x 2 = 20\n", + "10 x 4 = 40\n", + "10 x 6 = 60\n", + "10 x 8 = 80\n", + "10 x 10 = 100\n", + "10 x 12 = 120\n", + "10 x 14 = 140\n", + "10 x 16 = 160\n", + "10 x 18 = 180\n", + "10 x 20 = 200\n" + ] + } + ], + "source": [ + "n = 10\n", + "for i in range(1,n+1):\n", + " print(n,\"x\",i*2,\"=\",n*i*2)\n" + ] }, { "cell_type": "markdown", @@ -59,10 +80,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 x 2 = 40\n", + "10 x 4 = 80\n", + "10 x 6 = 120\n", + "10 x 8 = 160\n", + "10 x 10 = 200\n", + "10 x 12 = 240\n", + "10 x 14 = 280\n", + "10 x 16 = 320\n", + "10 x 18 = 360\n", + "10 x 20 = 400\n" + ] + } + ], + "source": [ + "n = 10\n", + "for i in range(2,n*2+1, 2):\n", + " print(n,\"x\",i,\"=\",n*i*2)" + ] }, { "cell_type": "markdown", @@ -84,10 +126,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 x 1 = 20\n", + "10 x 2 = 40\n", + "10 x 3 = 60\n", + "10 x 4 = 80\n", + "10 x 5 = 100\n", + "10 x 6 = 120\n", + "10 x 7 = 140\n", + "10 x 8 = 160\n", + "10 x 9 = 180\n", + "10 x 10 = 200\n" + ] + }, + { + "data": { + "text/plain": [ + "['10x1=20',\n", + " '10x2=40',\n", + " '10x3=60',\n", + " '10x4=80',\n", + " '10x5=100',\n", + " '10x6=120',\n", + " '10x7=140',\n", + " '10x8=160',\n", + " '10x9=180',\n", + " '10x10=200']" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "n = 10\n", + "[print(n,\"x\",i,\"=\",n*i*2) for i in range(1,n+1)]\n", + "\n", + "# To get a list of strings\n", + "[str(n)+\"x\"+str(i)+\"=\"+str(n*i*2) for i in range(1,n+1)]\n" + ] }, { "cell_type": "markdown", @@ -98,10 +182,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['10 x 1 = 20',\n", + " '10 x 2 = 40',\n", + " '10 x 3 = 60',\n", + " '10 x 4 = 80',\n", + " '10 x 5 = 100',\n", + " '10 x 6 = 120',\n", + " '10 x 7 = 140',\n", + " '10 x 8 = 160',\n", + " '10 x 9 = 180',\n", + " '10 x 10 = 200']" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = \"{0} x {1} = {2}\"\n", + "[s.format(*[n,i,n*i*2]) for i in range(1,n+1)]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 x 2 = 20\n", + "10 x 4 = 40\n", + "10 x 6 = 60\n", + "10 x 8 = 80\n", + "10 x 10 = 100\n", + "10 x 12 = 120\n", + "10 x 14 = 140\n", + "10 x 16 = 160\n", + "10 x 18 = 180\n", + "10 x 20 = 200\n" + ] + } + ], + "source": [ + "#OR \n", + "s = \"{0} x {1} = {2}\"\n", + "for i in range(1,n+1):\n", + " print(s.format(*[n,i*2,n*i*2]))" + ] }, { "cell_type": "markdown", @@ -112,10 +248,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'10 x 2 = 2010 x 4 = 4010 x 6 = 6010 x 8 = 8010 x 10 = 10010 x 12 = 12010 x 14 = 14010 x 16 = 16010 x 18 = 18010 x 20 = 200'" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = \"\"\n", + "s = \"{0} x {1} = {2}\"\n", + "#[s.format(*[n,i,n*i*2]) for i in range(1,n+1)]\n", + "l.join([s.format(*[n,i*2,n*i*2]) for i in range(1,n+1)])" + ] }, { "cell_type": "markdown", @@ -126,10 +278,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'10 x 1 = 20 \\n10 x 2 = 40 \\n10 x 3 = 60 \\n10 x 4 = 80 \\n10 x 5 = 100 \\n10 x 6 = 120 \\n10 x 7 = 140 \\n10 x 8 = 160 \\n10 x 9 = 180 \\n10 x 10 = 200 \\n'" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = \"\"\n", + "s = \"{0} x {1} = {2} {3}\"\n", + "#[s.format(*[n,i,n*i*2]) for i in range(1,n+1)]\n", + "l.join([s.format(*[n,i,n*i*2],'\\n') for i in range(1,n+1)])" + ] }, { "cell_type": "markdown", @@ -145,10 +313,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 102, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[(10, 20, 200), (40, 50, 2000), (70, 80, 5600)]" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]\n", + "\n", + "res =[]\n", + "for tup in l:\n", + " res+=[(tup[0],tup[1],tup[0]*tup[1])]\n", + "res" + ] }, { "cell_type": "markdown", @@ -163,10 +351,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[(10, 20, 100, 200), (40, 50, 2000), (70, 80, 100, 200, 300, 5600)]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l = [(10, 20, 100, 40), (40, 50, 60), (70, 80, 100, 200, 300, 90)]\n", + "\n", + "res =[]\n", + "for tup in l:\n", + " tmp = (tup[0:len(tup)-1]) + (tup[0]*tup[1],) \n", + " res+=[tmp]\n", + " \n", + "res" + ] }, { "cell_type": "markdown", @@ -191,10 +399,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Smallest word: A \n", + "Largest word: quick \n" + ] + } + ], + "source": [ + "string = \"A quick red fox\"\n", + "lis =[]\n", + "minw=\"\"\n", + "maxw=\"\"\n", + "tmp = \"\"\n", + "for w in string:\n", + " tmp+=w \n", + " if(w==\" \" or string.index(w)==len(string)-1):\n", + " lis+=[tmp]\n", + " tmp = \"\"\n", + "\n", + "if len(lis)>0:\n", + " minw= lis[0]\n", + "for i in lis:\n", + " if(len(i)< len(minw)):\n", + " minw = i\n", + " if(len(i)> len(maxw)):\n", + " maxw = i\n", + " \n", + "print(\"Smallest word: \" + minw + \"\\nLargest word: \" + maxw)" + ] }, { "cell_type": "markdown", @@ -207,10 +445,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Smallest word: A\n", + "Largest word: quick\n" + ] + } + ], + "source": [ + "string = \"A quick red fox\"\n", + "lis = string.split(\" \")\n", + "minw= lis[0]\n", + "maxw=\"\"\n", + "for i in lis:\n", + " if(len(i)< len(minw)):\n", + " minw = i\n", + " if(len(i)> len(maxw)):\n", + " maxw = i\n", + " \n", + "print(\"Smallest word: \" + minw + \"\\nLargest word: \" + maxw)" + ] }, { "cell_type": "markdown", @@ -223,25 +482,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", + "execution_count": 75, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Smallest word: A\n", + "Largest word: quick\n" + ] + } + ], "source": [ - "At the end of the day, the exercise can be solved in the following way:" + "string = \"A quick red fox\"\n", + "lis = string.split(\" \") \n", + "print(\"Smallest word: \" + min(lis,key=len) + \"\\nLargest word: \" + max(lis,key=len))" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -264,10 +522,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[[30, 56, 25], [33]]" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ls = [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]]\n", + "\n", + "no_dup = ls.copy()\n", + "\n", + "for i in range(len(ls)):\n", + " for j in range(len(ls)):\n", + " #print(\"compare :\" + str(ls[i]) +\" - \"+ str(ls[j]))\n", + " if(i!=j and ls[i]==ls[j]):\n", + " no_dup.remove(ls[i])\n", + "\n", + "no_dup" + ] }, { "cell_type": "markdown", @@ -302,13 +583,6 @@ "outputs": [], "source": [] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -323,13 +597,6 @@ "outputs": [], "source": [] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, @@ -351,10 +618,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 148, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['Theo', 'Ralph', 'Ralph']" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "students = [{'id': 1, 'success': True, 'name': 'Theo'},\n", + " {'id': 2, 'success': False, 'name': 'Alex'},\n", + " {'id': 3, 'success': True, 'name': 'Ralph'},\n", + " {'id': 4, 'success': True, 'name': 'Ralph'},\n", + " {'id': 5, 'success': False, 'name': 'Theo'}]\n", + "\n", + "lis = []\n", + "for dic in students:\n", + " if(dic[\"success\"] == True):\n", + " lis+=[dic[\"name\"]]\n", + "lis\n", + "\n" + ] }, { "cell_type": "markdown", @@ -368,7 +659,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#OR\n", + "[dic[\"name\"] for dic in students if(dic[\"success\"] == True)]" + ] }, { "cell_type": "markdown", @@ -408,7 +702,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.7.4" } }, "nbformat": 4,