From 83e6a31b2bc17973311fdb493871f672a367977d Mon Sep 17 00:00:00 2001 From: andreadeans Date: Tue, 9 Oct 2018 16:22:56 +0200 Subject: [PATCH 1/3] 00ex --- 00ex_andreadeans.ipynb | 350 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 00ex_andreadeans.ipynb diff --git a/00ex_andreadeans.ipynb b/00ex_andreadeans.ipynb new file mode 100644 index 0000000..2b43e49 --- /dev/null +++ b/00ex_andreadeans.ipynb @@ -0,0 +1,350 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. The MickeyMouse problem\n", + "\n", + "a) Write a program that prints the numbers from 1 to 100. But for multiples of three print Mickey instead of the number and for the multiples of five print Mouse. For numbers which are multiples of both three and five print MickeyMouse\n", + "\n", + "b) Put the result in a tuple and substitute Mickey with Donald and Mouse with Duck" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "Mickey\n", + "4\n", + "Mouse\n", + "Mickey\n", + "7\n", + "8\n", + "Mickey\n", + "Mouse\n", + "11\n", + "Mickey\n", + "13\n", + "14\n", + "Mickey Mouse\n", + "16\n", + "17\n", + "Mickey\n", + "19\n", + "Mouse\n", + "Mickey\n", + "22\n", + "23\n", + "Mickey\n", + "Mouse\n", + "26\n", + "Mickey\n", + "28\n", + "29\n", + "Mickey Mouse\n", + "31\n", + "32\n", + "Mickey\n", + "34\n", + "Mouse\n", + "Mickey\n", + "37\n", + "38\n", + "Mickey\n", + "Mouse\n", + "41\n", + "Mickey\n", + "43\n", + "44\n", + "Mickey Mouse\n", + "46\n", + "47\n", + "Mickey\n", + "49\n", + "Mouse\n", + "Mickey\n", + "52\n", + "53\n", + "Mickey\n", + "Mouse\n", + "56\n", + "Mickey\n", + "58\n", + "59\n", + "Mickey Mouse\n", + "61\n", + "62\n", + "Mickey\n", + "64\n", + "Mouse\n", + "Mickey\n", + "67\n", + "68\n", + "Mickey\n", + "Mouse\n", + "71\n", + "Mickey\n", + "73\n", + "74\n", + "Mickey Mouse\n", + "76\n", + "77\n", + "Mickey\n", + "79\n", + "Mouse\n", + "Mickey\n", + "82\n", + "83\n", + "Mickey\n", + "Mouse\n", + "86\n", + "Mickey\n", + "88\n", + "89\n", + "Mickey Mouse\n", + "91\n", + "92\n", + "Mickey\n", + "94\n", + "Mouse\n", + "Mickey\n", + "97\n", + "98\n", + "Mickey\n", + "Mouse\n" + ] + } + ], + "source": [ + "#1.a\n", + "\n", + "for i in range (1,101,1):\n", + " if i % 3 == 0 and i % 5 == 0: print(\"Mickey Mouse\")\n", + " elif i % 3 == 0: print (\"Mickey\")\n", + " elif i % 5 == 0: print(\"Mouse\")\n", + " else: print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 'Donald', 4, 'Duck', 'Donald', 7, 8, 'Donald', 'Duck', 11, 'Donald', 13, 14, 'Donald Duck', 16, 17, 'Donald', 19, 'Duck', 'Donald', 22, 23, 'Donald', 'Duck', 26, 'Donald', 28, 29, 'Donald Duck', 31, 32, 'Donald', 34, 'Duck', 'Donald', 37, 38, 'Donald', 'Duck', 41, 'Donald', 43, 44, 'Donald Duck', 46, 47, 'Donald', 49, 'Duck', 'Donald', 52, 53, 'Donald', 'Duck', 56, 'Donald', 58, 59, 'Donald Duck', 61, 62, 'Donald', 64, 'Duck', 'Donald', 67, 68, 'Donald', 'Duck', 71, 'Donald', 73, 74, 'Donald Duck', 76, 77, 'Donald', 79, 'Duck', 'Donald', 82, 83, 'Donald', 'Duck', 86, 'Donald', 88, 89, 'Donald Duck', 91, 92, 'Donald', 94, 'Duck', 'Donald', 97, 98, 'Donald', 'Duck']\n" + ] + } + ], + "source": [ + "#1.b\n", + "a = []\n", + "for i in range (1,101,1):\n", + " if i % 3 == 0 and i % 5 == 0: a.append(\"Mickey Mouse\")\n", + " elif i % 3 == 0: a.append(\"Mickey\")\n", + " elif i % 5 == 0: a.append(\"Mouse\")\n", + " else: a.append(i)\n", + "for i in range (1,101,1):\n", + " if i % 3 == 0 and i % 5 == 0: a[i-1] = \"Donald Duck\"\n", + " elif i % 3 == 0: a[i-1] = \"Donald\"\n", + " elif i % 5 == 0: a[i-1] = \"Duck\"\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2\\. The swap function\n", + "\n", + "Write a function that swap the values of two input variables x and y (whatever the type). Try to do that also without a temporary variable" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hallo\n", + "4\n" + ] + } + ], + "source": [ + "#2\n", + "x = 4\n", + "y = 'hallo'\n", + "def swap(a,b):\n", + " return b,a\n", + "x,y = swap(x,y)\n", + "print(x)\n", + "print(y)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3\\. Computing the distance\n", + "\n", + "Write a function that calculates and returns the euclidean distance between two points *u* and *v*, where *u* and *v* are both 2-tuples *(x,y)*. For example, if *u=(3,0)* and *v=(0,4)*, the function should return 5" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.0" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#3\n", + "u = [3,0]\n", + "v = [0,4]\n", + "import math\n", + "def distance(x,y):\n", + " return math.sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1]))\n", + "distance(u,v)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4\\. Counting letters\n", + "\n", + "Write a program to calculate the number of times each character occurs in a given string *s*. Ignore differneces in capitalization" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "s=\"Write a program that prints the numbers from 1 to 100. \\\n", + "But for multiples of three print Mickey instead of the number and for the multiples of five print Mouse. \\\n", + "For numbers which are multiples of both three and five print MickeyMouse\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5\\. Isolating the unique\n", + "\n", + "Write a function that determines and count the unique numbers in the list *l*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "l = [36, 45, 58, 3, 74, 96, 64, 45, 31, 10, 24, 19, 33, 86, 99, 18, 63, 70, 85,\n", + " 85, 63, 47, 56, 42, 70, 84, 88, 55, 20, 54, 8, 56, 51, 79, 81, 57, 37, 91,\n", + " 1, 84, 84, 36, 66, 9, 89, 50, 42, 91, 50, 95, 90, 98, 39, 16, 82, 31, 92, 41,\n", + " 45, 30, 66, 70, 34, 85, 94, 5, 3, 36, 72, 91, 84, 34, 87, 75, 53, 51, 20, 89, 51, 20]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6\\. Combination of functions\n", + "\n", + "Write two functions - one that returns the square of a number, and one that returns the cube. Now write a third function that returns the number raised to the 6th power using the two previous functions." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7\\. Cubes\n", + "\n", + "Create a list of the cubes of x for x in *[0, 10]* using:\n", + "\n", + "a) a for loop\n", + "\n", + "b) a list comprehension" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8\\. Nested list comprehension\n", + "\n", + "A Pythagorean triple is an integer solution to the Pythagorean theorem $a^2+b^2=c^2$. The first Pythagorean triple is (3,4,5). Find and put in a tuple all unique Pythagorean triples for the positive integers a, b and c less than 100." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "9\\. Normalization\n", + "\n", + "Write a function that takes a tuple of numbers and returns it with the entries normalized to one" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "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.6.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From dc3fde573b9b9c49e9ffe01c0e1aa26f3951542f Mon Sep 17 00:00:00 2001 From: andreadeans Date: Fri, 12 Oct 2018 15:03:06 +0200 Subject: [PATCH 2/3] save file --- 00ex_andreadeans.ipynb | 343 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 323 insertions(+), 20 deletions(-) diff --git a/00ex_andreadeans.ipynb b/00ex_andreadeans.ipynb index 2b43e49..a1a2202 100644 --- a/00ex_andreadeans.ipynb +++ b/00ex_andreadeans.ipynb @@ -206,7 +206,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 95, "metadata": {}, "outputs": [ { @@ -215,7 +215,7 @@ "5.0" ] }, - "execution_count": 57, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -226,9 +226,11 @@ "v = [0,4]\n", "import math\n", "def distance(x,y):\n", + " try:math.sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1]))\n", + " except: \n", + " print(\"I need two-elements lists as arguments!\")\n", " return math.sqrt((x[0]-y[0])*(x[0]-y[0])+(x[1]-y[1])*(x[1]-y[1]))\n", - "distance(u,v)\n", - "\n" + "distance(u,v)" ] }, { @@ -242,15 +244,91 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "metadata": { - "collapsed": true + "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(' ', 41)\n", + "('.', 2)\n", + "('0', 2)\n", + "('1', 2)\n", + "('a', 7)\n", + "('b', 5)\n", + "('c', 3)\n", + "('d', 3)\n", + "('e', 22)\n", + "('f', 10)\n", + "('g', 1)\n", + "('h', 9)\n", + "('i', 14)\n", + "('k', 2)\n", + "('l', 6)\n", + "('m', 12)\n", + "('n', 10)\n", + "('o', 13)\n", + "('p', 8)\n", + "('r', 17)\n", + "('s', 9)\n", + "('t', 19)\n", + "('u', 9)\n", + "('v', 2)\n", + "('w', 2)\n", + "('y', 2)\n" + ] + } + ], + "source": [ + "s=\"Write a program that prints the numbers from 1 to 100. \\\n", + "But for multiples of three print Mickey instead of the number and for the multiples of five print Mouse. \\\n", + "For numbers which are multiples of both three and five print MickeyMouse\"\n", + "\n", + "#4\n", + "s = s.lower()\n", + "#print(s)\n", + "ch = [x for x in s]\n", + "ch.sort()\n", + "#print(ch)\n", + "j = 0\n", + "for i in range (1, len(ch),1):\n", + " j = j+1\n", + " if ch[i-1] != ch[i] and ch[i-1] != ch[len(ch)-1]:\n", + " print(ch[i-1], j)\n", + " j = 0\n", + " elif ch[i-1] == ch[len(ch)-1]: \n", + " print(ch[len(ch)-1],len(ch)-i+1)" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{' ': 41, '.': 2, '1': 2, '0': 2, 'a': 7, 'c': 3, 'b': 5, 'e': 22, 'd': 3, 'g': 1, 'f': 10, 'i': 14, 'h': 9, 'k': 2, 'm': 12, 'l': 6, 'o': 13, 'n': 10, 'p': 8, 's': 9, 'r': 17, 'u': 9, 't': 19, 'w': 2, 'v': 2, 'y': 2}\n" + ] + } + ], "source": [ + "#4.extra\n", "s=\"Write a program that prints the numbers from 1 to 100. \\\n", "But for multiples of three print Mickey instead of the number and for the multiples of five print Mouse. \\\n", - "For numbers which are multiples of both three and five print MickeyMouse\"" + "For numbers which are multiples of both three and five print MickeyMouse\"\n", + "s = s.lower()\n", + "dic = {}\n", + "for i in range(0,len(s),1):\n", + " dic[s[i]] = 0\n", + " \n", + "for i in range(0,len(s),1):\n", + " dic[s[i]] = dic[s[i]] + 1 \n", + "print(dic)" ] }, { @@ -264,16 +342,78 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "5\n", + "8\n", + "9\n", + "10\n", + "16\n", + "18\n", + "19\n", + "24\n", + "30\n", + "33\n", + "37\n", + "39\n", + "41\n", + "47\n", + "53\n", + "54\n", + "55\n", + "57\n", + "58\n", + "64\n", + "72\n", + "74\n", + "75\n", + "79\n", + "81\n", + "82\n", + "86\n", + "87\n", + "88\n", + "90\n", + "92\n", + "94\n", + "95\n", + "96\n", + "98\n", + "99\n", + "There is a total of 37 unique numbers\n" + ] + } + ], "source": [ "l = [36, 45, 58, 3, 74, 96, 64, 45, 31, 10, 24, 19, 33, 86, 99, 18, 63, 70, 85,\n", " 85, 63, 47, 56, 42, 70, 84, 88, 55, 20, 54, 8, 56, 51, 79, 81, 57, 37, 91,\n", " 1, 84, 84, 36, 66, 9, 89, 50, 42, 91, 50, 95, 90, 98, 39, 16, 82, 31, 92, 41,\n", - " 45, 30, 66, 70, 34, 85, 94, 5, 3, 36, 72, 91, 84, 34, 87, 75, 53, 51, 20, 89, 51, 20]" + " 45, 30, 66, 70, 34, 85, 94, 5, 3, 36, 72, 91, 84, 34, 87, 75, 53, 51, 20, 89, 51, 20]\n", + "\n", + "#5\n", + "\n", + "def unique(a):\n", + " j = 0\n", + " a.sort()\n", + " counter = {}\n", + " for i in range(0,len(a),1):\n", + " counter[a[i]] = 0\n", + " for i in range(0,len(a),1):\n", + " counter[a[i]] = counter[a[i]] + 1 \n", + " for i in range(0,len(a),1):\n", + " if counter[a[i]] == 1:\n", + " j = j+1\n", + " print(a[i])\n", + " print(\"There is a total of %d unique numbers\" %j)\n", + " return\n", + "\n", + "unique(l)\n" ] }, { @@ -285,6 +425,39 @@ "Write two functions - one that returns the square of a number, and one that returns the cube. Now write a third function that returns the number raised to the 6th power using the two previous functions." ] }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "64" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#6\n", + "a = 3\n", + "b = 2\n", + "\n", + "def sq(x):\n", + " return x*x\n", + "\n", + "def cube(x):\n", + " return x*x*x\n", + "\n", + "def sixth(x,sqfunc,cubefunc):\n", + " return sqfunc(cubefunc(x))\n", + " \n", + "sixth(b,sq,cube)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -298,6 +471,33 @@ "b) a list comprehension" ] }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]\n", + "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]\n" + ] + } + ], + "source": [ + "#7.a\n", + "a =[]\n", + "for x in range(0,11,1):\n", + " a.append(x*x*x)\n", + "print(a)\n", + "\n", + "#7.b\n", + "cubes = []\n", + "cubes = [x*x*x for x in range(0,11,1)]\n", + "print (cubes)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -307,6 +507,79 @@ "A Pythagorean triple is an integer solution to the Pythagorean theorem $a^2+b^2=c^2$. The first Pythagorean triple is (3,4,5). Find and put in a tuple all unique Pythagorean triples for the positive integers a, b and c less than 100." ] }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(3, 4, 5),\n", + " (5, 12, 13),\n", + " (6, 8, 10),\n", + " (7, 24, 25),\n", + " (8, 15, 17),\n", + " (9, 12, 15),\n", + " (9, 40, 41),\n", + " (10, 24, 26),\n", + " (11, 60, 61),\n", + " (12, 16, 20),\n", + " (12, 35, 37),\n", + " (13, 84, 85),\n", + " (14, 48, 50),\n", + " (15, 20, 25),\n", + " (15, 36, 39),\n", + " (16, 30, 34),\n", + " (16, 63, 65),\n", + " (18, 24, 30),\n", + " (18, 80, 82),\n", + " (20, 21, 29),\n", + " (20, 48, 52),\n", + " (21, 28, 35),\n", + " (21, 72, 75),\n", + " (24, 32, 40),\n", + " (24, 45, 51),\n", + " (24, 70, 74),\n", + " (25, 60, 65),\n", + " (27, 36, 45),\n", + " (28, 45, 53),\n", + " (30, 40, 50),\n", + " (30, 72, 78),\n", + " (32, 60, 68),\n", + " (33, 44, 55),\n", + " (33, 56, 65),\n", + " (35, 84, 91),\n", + " (36, 48, 60),\n", + " (36, 77, 85),\n", + " (39, 52, 65),\n", + " (39, 80, 89),\n", + " (40, 42, 58),\n", + " (40, 75, 85),\n", + " (42, 56, 70),\n", + " (45, 60, 75),\n", + " (48, 55, 73),\n", + " (48, 64, 80),\n", + " (51, 68, 85),\n", + " (54, 72, 90),\n", + " (57, 76, 95),\n", + " (60, 63, 87),\n", + " (65, 72, 97)]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#8\n", + "import math\n", + "\n", + "i,j = 0,0\n", + "[(i,j,int(math.sqrt(i*i+j*j))) for i in range(1,100,1) for j in range(i,100,1) if (i != j and math.sqrt(i*i+j*j) < 100 and math.sqrt(i*i+j*j)%1 == 0)]\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -316,12 +589,42 @@ "Write a function that takes a tuple of numbers and returns it with the entries normalized to one" ] }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.5, 0.5, 0.5, 0.5]" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#9 \n", + "def norm(a):\n", + " den = 0\n", + " x = 0\n", + " for i in range(0,len(a),1):\n", + " den = den + a[i]*a[i]\n", + " den = math.sqrt(den)\n", + " a = [x/den for x in a]\n", + " return a\n", + "\n", + "x = [1,1,1,1]\n", + "\n", + "norm(x)" + ] + }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -335,14 +638,14 @@ "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.3" + "pygments_lexer": "ipython2", + "version": "2.7.15rc1" } }, "nbformat": 4, From bcea787a9de9b1bf8f555761166ce7510c39bac4 Mon Sep 17 00:00:00 2001 From: andreadeans Date: Fri, 12 Oct 2018 15:28:50 +0200 Subject: [PATCH 3/3] save file --- 00ex_andreadeans.ipynb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/00ex_andreadeans.ipynb b/00ex_andreadeans.ipynb index a1a2202..e30ba34 100644 --- a/00ex_andreadeans.ipynb +++ b/00ex_andreadeans.ipynb @@ -591,7 +591,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -600,13 +600,14 @@ "[0.5, 0.5, 0.5, 0.5]" ] }, - "execution_count": 45, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#9 \n", + "import math\n", "def norm(a):\n", " den = 0\n", " x = 0\n",