\n", "- \"A\" if the grade is above or equal 100, \n", "- \"B\" if the grade is from 90 to 99\n", "- \"C\" if the grade is from 80 to 89\n", "- \"D\" if the grade is from 66 to 79\n", "- \"F\" otherwise.\n", "\n", "For example: convertGrade(78) returns \"D\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "D\n" ] } ], "source": [ "def convertGrade(n):\n", " if 100<=n<=110:\n", " return \"A\"\n", " elif 90<=n<=99:\n", " return \"B\"\n", " elif 80<=n<=89:\n", " return \"B\"\n", " elif 66<=n<80:\n", " return \"D\"\n", " else:\n", " return \"F\"\n", "print(convertGrade(78))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Difficulty 2\n", "\n", "Build function even which takes as parameter an integer number. If the number is positive, the function returns **True** if the number is even and **False** if it is odd. If it is not positive, the function

\n", "\n", "Strategy: to check whether a number

1\n", "

22\n", "

333\n", "

4444\n", "

55555\n", "

666666\n", "

7777777\n", "

88888888\n", "\n", "Then draw the flowchart of this function.\n", "\n", "Then state and comment the computational complexity of this function only for strategy 2 in terms of int additions and string concatenations.\n", "\n", "\n", "Strategy 1: you have to set up one loop. Inside you build the string using string multiplication and printing the string immediately at each iteration of the loop.\n", " \n", "Strategy 2: you have to set up two while loops, using i as index for the external one and j as index for the internal one. I suggest to start both index from 1 and not 0. In the internal one you build the string attaching the numbers one after the other i times. At the end of the external one you print the string." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "22\n", "333\n", "4444\n", "55555\n", "666666\n", "7777777\n", "88888888\n", "1\n", "22\n", "333\n", "4444\n", "55555\n", "666666\n", "7777777\n", "88888888\n" ] } ], "source": [ "def triangle1(N):\n", " i=1\n", " while(i<=N):\n", " j=1\n", " s=str(i)\n", " print(s*i)\n", " i+=1\n", "\n", " \n", "def triangle2(N):\n", " i=1\n", " while(i<=N):\n", " j=1\n", " s=str(i)\n", " while(j

You may not use methods .sort() and .index() in this exercises, otherwise it is difficulty 1.\n", "\n", "For example: secondMax([4,9,0,-4,2,-8]) returns 4, secondMax([9,9,0,-4,2,-8]) returns 9\n", "\n", "Strategy: you set up two variables in which you store the temporary maximum and the temporary second maximum. You set the value of these variables equal to the first and the second element of the list, depending on which one is the largest of the two. Then you go through all the other elements of the list and if that element is larger than your temporary maximum you have to update both your temporary values (pay attention to the switch), while if that value is larger than your second maximum you have to update only that value. After having written it, fix the issue when the largest two numbers have the same value." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "9\n" ] } ], "source": [ "def secondMax(L):\n", " if L[0]>L[1]:\n", " tmax=L[0]\n", " tmax2=L[1]\n", " else:\n", " tmax=L[1]\n", " tmax2=L[0]\n", " for x in L[2:]:\n", " if x>=tmax: # the = here fixes the situation in which the two largest elements are equal\n", " tmax2=tmax # this can be written more easily with tmax2,tmax=tmax,x\n", " tmax=x\n", " elif x>tmax2:\n", " tmax2=x\n", " return tmax2\n", "\n", "print(secondMax([4,9,0,-4,2,-8]))\n", "print(secondMax([9,9,0,-4,2,-8]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Difficulty: 6\n", "\n", "Build function divisibleAll which takes as input an integer positive number N and a list of integer positive numbers L. It returns a list of all the numbers between 1 and N which are divisible by ALL the numbers in the list L.\n", "\n", "For example: divisibleAll(50, [ 3 , 5 ]) returns [ 15 , 30 , 45 ]\n", "\n", "Strategy: set up an empty list. Go through all the numbers from 1 to N and set up a boolean variable to True. Go through all the elements of the list. If at least one element of the list is not a divisor of the number, set the boolean variable to False. At the end of the loop on the elements of the list, if the boolean variable is True append that number to your list. At the end of the external loop, return your list." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[15, 30, 45]\n" ] } ], "source": [ "def divisibleAll(N,L):\n", " R=[]\n", " for x in list(range(1,N+1,1)):\n", " flag=True\n", " for y in L:\n", " if x%y!=0:\n", " flag=False\n", " break\n", " if flag:\n", " R.append(x)\n", " return R\n", "print(divisibleAll(50, [ 3 , 5 ]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Difficulty 4\n", "\n", "Build function mergeEqual which takes as input two lists of equal length and returns a list made up of alternating elements of the two lists.\n", "\n", "For example, merge([3,4,5],[9,10,11]) returns [3,9,4,10,5,11]\n", "\n", "Strategy: you set up an empty list. Then you go through the indexes from 0 to the length of the input lists and at each step append the element from each of the two lists." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3, 9, 4, 10, 5, 11]\n" ] } ], "source": [ "def mergeEqual(L1,L2):\n", " i=0\n", " L=[]\n", " while(i

M= [ [1,2,3,4] , [5,6,7,8] , [9,10,11,12] ]\n", "

transpose(M) returns [ [1,5,9] , [2,6,10] , [3,7,11] , [4,8,12] ]\n", "\n", "State and comment the complexity of this function.\n", "\n", "Strategy: first you find out the width

Stop here and check whether this temporary result is correct. If it is, go on writing your code.\n", "

At this point you go through the elements of

[ [0,0,0,0] , [0,1,2,3] , [0,2,4,6] , [0,3,6,9] , [0,4,8,12] ]\n", "

where the numbers are calculates as\n", "

[ [ 0 * 0 , 0 * 1 , 0 * 2 , 0 * 3 ] , [ 1 * 0 , 1 * 1 , 1 * 2 , 1 * 3 ] , , [ 2 * 0 , 2 * 1 , 2 * 2 , 2 * 3 ] , [ 3 * 0 , 3 * 1 , 3 * 2 , 3 * 3 ] , [ 4 * 0 , 4 * 1 , 4 * 2 , 4 * 3 ] ]\n", "\n", "State and comment the computational complexity of this function.\n", "\n", "Strategy: first it is better to create an empty list