diff --git a/100Python3Exercises.ipynb b/100Python3Exercises.ipynb new file mode 100644 index 00000000..fcc6a333 --- /dev/null +++ b/100Python3Exercises.ipynb @@ -0,0 +1,922 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 1\n", + "## Level 1\n", + "**Question:** Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line.\n", + "\n", + "**Hints:** Consider use range(#begin, #end) method\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199\n" + ] + } + ], + "source": [ + "l=[]\n", + "for i in range(2000,3201):\n", + " if (i%7==0) and (i%5!=0):\n", + " l.append(str(i))\n", + "print(','.join(l))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 2\n", + "## Level 1\n", + "**Question:** Write a program which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program: 8 Then, the output should be: 40320\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "720\n" + ] + } + ], + "source": [ + "from math import factorial\n", + "n=int(input())\n", + "print(factorial(n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 3\n", + "## Level 1\n", + "**Question:** With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary. Suppose the following input is supplied to the program: 8 Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input. Consider use dict()\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}\n" + ] + } + ], + "source": [ + "n=int(input())\n", + "d=dict()\n", + "for i in range(1,n+1):\n", + " d[i]=i*i\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 4\n", + "## Level 1\n", + "**Question:** Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34,67,55,33,12,98 Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input. tuple() method can convert list to tuple\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "34,67,55,33,12,98\n", + "['34', '67', '55', '33', '12', '98']\n", + "('34', '67', '55', '33', '12', '98')\n" + ] + } + ], + "source": [ + "l=input().split(',')\n", + "print(l)\n", + "print(tuple(l))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 5\n", + "## Level 1\n", + "\n", + "**Question:** Define a class which has at least two methods: getString: to get a string from console input printString: to print the string in upper case. Also please include simple test function to test the class methods.\n", + "\n", + "**Hints:** Use init method to construct some parameters\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world!\n", + "HELLO WORLD!\n" + ] + } + ], + "source": [ + "class InputOutString(object):\n", + " \n", + " def __init__(self):\n", + " self.s=\"\"\n", + " \n", + " def getString(self):\n", + " self.s=input()\n", + " \n", + " def printString(self):\n", + " print(self.s.upper())\n", + " \n", + "strObj=InputOutString()\n", + "strObj.getString()\n", + "strObj.printString()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 6\n", + "## Level 2\n", + "\n", + "**Question:** Write a program that calculates and prints the value according to the given formula: Q = Square root of [(2 * C * D)/H] Following are the fixed values of C and H: C is 50. H is 30. D is the variable whose values should be input to your program in a comma-separated sequence. Example Let us assume the following comma separated input sequence is given to the program: 100,150,180 The output of the program should be: 18,22,24\n", + "\n", + "**Hints:** If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26) In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18,22,24\n" + ] + } + ], + "source": [ + "import math\n", + "l = [100,150,180]\n", + "q = []\n", + "for i in l:\n", + " q.append(str(int(math.sqrt((2*50*i)/30))))\n", + "print(','.join(q))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 7\n", + "## Level 2\n", + "\n", + "**Question:** Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j. Note: i=0,1.., X-1; j=0,1,¡­Y-1. Example Suppose the following inputs are given to the program: 3,5 Then, the output of the program should be: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n", + "\n", + "**Hints:** Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4,6\n", + "[[ 0 0 0 0 0 0]\n", + " [ 0 1 2 3 4 5]\n", + " [ 0 2 4 6 8 10]\n", + " [ 0 3 6 9 12 15]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "items=[int(x) for x in input().split(',')]\n", + "x=items[0]\n", + "y=items[1]\n", + "da=np.arange(x*y).reshape(x,y)\n", + "for i in range(x):\n", + " for j in range(y):\n", + " da[i][j]=i*j\n", + "print(da)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 8\n", + "## Level 2\n", + "\n", + "**Question:** Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically. Suppose the following input is supplied to the program: without,hello,bag,world Then, the output should be: bag,hello,without,world\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "without,hello,bag,world\n", + "bag,hello,without,world\n" + ] + } + ], + "source": [ + "s=[x for x in input().split(',')]\n", + "s.sort()\n", + "print(','.join(s))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 9\n", + "## Level 2\n", + "\n", + "**Question:** Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized. Suppose the following input is supplied to the program: Hello world Practice makes perfect Then, the output should be: HELLO WORLD PRACTICE MAKES PERFECT\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world Practice makes perfect \n", + "HELLO WORLD PRACTICE MAKES PERFECT \n" + ] + } + ], + "source": [ + "print(input().upper())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Another solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello world Practice makes perfect \n", + "\n", + "HELLO WORLD PRACTICE MAKES PERFECT \n" + ] + } + ], + "source": [ + "lines=[]\n", + "while True:\n", + " s=input()\n", + " if s:\n", + " lines.append(s.upper())\n", + " else:\n", + " break;\n", + "for sentence in lines:\n", + " print(sentence)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 10\n", + "## Level 2\n", + "\n", + "**Question:** Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically. Suppose the following input is supplied to the program: hello world and practice makes perfect and hello world again Then, the output should be: again and hello makes perfect practice world\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input. We use set container to remove duplicated data automatically and then use sorted() to sort the data.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world and practice makes perfect and hello world again\n", + "again and hello makes perfect practice world\n" + ] + } + ], + "source": [ + "print(' '.join(sorted(set([word for word in input().split(' ')]))))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 11\n", + "## Level 2\n", + "\n", + "**Question:** Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence. Example: 0100,0011,1010,1001 Then the output should be: 1010 Notes: Assume the data is input by console.\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0100,0011,1010,1001\n", + "1010\n" + ] + } + ], + "source": [ + "values = [number for number in input().split(',')]\n", + "result=[]\n", + "for number in values:\n", + " number_p=int(number,2)\n", + " if not number_p%5:\n", + " result.append(number)\n", + "print(','.join(result))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 12\n", + "## Level 2\n", + "\n", + "**Question:** Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number. The numbers obtained should be printed in a comma-separated sequence on a single line.\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2000,2002,2004,2006,2008,2020,2022,2024,2026,2028,2040,2042,2044,2046,2048,2060,2062,2064,2066,2068,2080,2082,2084,2086,2088,2200,2202,2204,2206,2208,2220,2222,2224,2226,2228,2240,2242,2244,2246,2248,2260,2262,2264,2266,2268,2280,2282,2284,2286,2288,2400,2402,2404,2406,2408,2420,2422,2424,2426,2428,2440,2442,2444,2446,2448,2460,2462,2464,2466,2468,2480,2482,2484,2486,2488,2600,2602,2604,2606,2608,2620,2622,2624,2626,2628,2640,2642,2644,2646,2648,2660,2662,2664,2666,2668,2680,2682,2684,2686,2688,2800,2802,2804,2806,2808,2820,2822,2824,2826,2828,2840,2842,2844,2846,2848,2860,2862,2864,2866,2868,2880,2882,2884,2886,2888\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def findEven(a,b):\n", + " l=[]\n", + " for i in range(a,b+1):\n", + " s=str(i)\n", + " if not (int(s[0])%2)|(int(s[1])%2)|(int(s[2])%2)|(int(s[3])%2):\n", + " l.append(s)\n", + " return l\n", + "\n", + "print(','.join(findEven(1000,3000)))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 13\n", + "## Level 2\n", + "\n", + "**Question:** Write a program that accepts a sentence and calculate the number of letters and digits. Suppose the following input is supplied to the program: hello world! 123 Then, the output should be: LETTERS 10 DIGITS 3\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Solution:**" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hello world! 123\n", + "LETTERS 10 DIGITS 3\n" + ] + } + ], + "source": [ + "sentence = input()\n", + "letters=0\n", + "digits=0\n", + "for s in sentence:\n", + " if 6412:\n", + " print('too long')\n", + "b1=False\n", + "b2=False\n", + "b3=False\n", + "for s in pw:\n", + " b1 = b1 | s.isalpha()\n", + " b2 = b2 | s.isdigit()\n", + " b3 = b3 |(s=='$')|(s=='#')|(s=='@')\n", + "if not b1:\n", + " print('no alpha')\n", + "if not b2:\n", + " print('no digit')\n", + "if not b3:\n", + " print('no $#@')\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Question 19\n", + "## Level 3\n", + "\n", + "**Question:** You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is: \n", + "1. Sort based on name; \n", + "2. Then sort based on age; \n", + "3. Then sort by score. \n", + "\n", + "The priority is that name > age > score.\n", + "If the following tuples are given as input to the program: Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85\n", + "Then, the output of the program should be: [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]\n", + "\n", + "**Hints:** In case of input data being supplied to the question, it should be assumed to be a console input. We use itemgetter to enable multiple sort keys.\n", + "\n", + "**Solutions:** from operator import itemgetter, attrgetter" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tom,19,80 John,20,90 Jony,17,91 Jony,17,93 Json,21,85\n", + "\n", + "[('Tom,19,80', 'John,20,90', 'Jony,17,91', 'Jony,17,93', 'Json,21,85')]\n" + ] + } + ], + "source": [ + "from operator import itemgetter\n", + "\n", + "l = []\n", + "while True:\n", + " s = input()\n", + " if not s:\n", + " break\n", + " l.append(tuple(s.split(' ')))\n", + " \n", + "print(sorted(l, key=itemgetter(0,1,2)))" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function sorted in module builtins:\n", + "\n", + "sorted(iterable, /, *, key=None, reverse=False)\n", + " Return a new list containing all items from the iterable in ascending order.\n", + " \n", + " A custom key function can be supplied to customize the sort order, and the\n", + " reverse flag can be set to request the result in descending order.\n", + "\n" + ] + } + ], + "source": [ + "help(sorted)" + ] + }, + { + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}