diff --git a/Lab Assignment 11_GAUTAM JAIN_E18CSE056.ipynb b/Lab Assignment 11_GAUTAM JAIN_E18CSE056.ipynb new file mode 100644 index 0000000..c2efbab --- /dev/null +++ b/Lab Assignment 11_GAUTAM JAIN_E18CSE056.ipynb @@ -0,0 +1,537 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python function \n", + "## Every solution should be done with the help of functions." + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Function is used for modularity in the program.\n", + "\n", + "def function_name(argument-1, argument-2,argument-3...):\n", + " function body\n", + " .\n", + " .\n", + " .\n", + " .\n", + " .\n", + " return var \n", + "#function call\n", + "function_name(10,20,30)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Write a program to compute gcd of two user input numbers." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First Number: 9\n", + "Second Number: 6\n", + "3\n" + ] + } + ], + "source": [ + "def gcd(a,b):\n", + " if(b==0):\n", + " return a\n", + " else:\n", + " return gcd(b,a%b)\n", + "x=int(input('First Number: '))\n", + "y=int(input('Second Number: '))\n", + "print(gcd(x,y))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2.Write a program to take an integer from the user and return reverse of the number.\n", + "### (Example: I/P 123, O/P: 321) " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "First Number: 234\n", + "432\n" + ] + } + ], + "source": [ + "def rev(a,y):\n", + " if(a<=0):\n", + " return y\n", + " else:\n", + " y=y*10+a%10\n", + " return rev(a//10,y)\n", + "x=int(input('First Number: '))\n", + "print(rev(x,0))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. Write a program in Python to print prime numbers between 25 and 100 using function." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 " + ] + } + ], + "source": [ + "def prime():\n", + " for i in range(25,101):\n", + " k=1\n", + " for j in range(2,i):\n", + " if(i%j==0):\n", + " k=0\n", + " break\n", + " if(k==1):\n", + " print(i,end=' ')\n", + "prime()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Define a Pyhton function pow1 that takes the values of two integers m and n from the user and calculate pow(m,n). Call this function with different values.\n", + "### Do not use exponent operator (**) or pow()" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter m: 2\n", + "Enter n: 4\n", + "16\n" + ] + } + ], + "source": [ + "def pow1(a,b,y):\n", + " if(b==1):\n", + " return a\n", + " else:\n", + " a*=y\n", + " return pow1(a,b-1,y)\n", + "m=int(input('Enter m: '))\n", + "n=int(input('Enter n: '))\n", + "print(pow1(m,n,m))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5. Write a Python program to find the factorial of a number using a recursive function." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a: 5\n", + "120\n" + ] + } + ], + "source": [ + "def fact(x,y):\n", + " if(y==1):\n", + " return x\n", + " else:\n", + " x=x*(y)\n", + " return fact(x,y-1)\n", + "a=int(input('Enter a: '))\n", + "print(fact(a,a-1))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 6. Design and develop a python function RightShift(x ,n) that takes two integers x and n as input and returns value of the integer x shifted to the right by n positions. Assume the integers are unsigned. \n", + "### Write a Python program that invokes this function with multiple values for x and n and tabulate the results with suitable headings withour using right shift operator." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x: 5\n", + "n: 2\n", + "2\n" + ] + } + ], + "source": [ + "def rightshift(a,b):\n", + " for i in range(1,b+1):\n", + " ans=a//2\n", + " return ans\n", + "x=int(input('x: '))\n", + "n=int(input('n: '))\n", + "print(rightshift(x,n))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 7. Write a program in python to find the sum of the series 1!/1+2!/2+3!/3+4!/4+5!/5 using the function. " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "34.0\n" + ] + } + ], + "source": [ + "import math\n", + "def sum():\n", + " s=0\n", + " for i in range(1,6):\n", + " s+=math.factorial(i)/i\n", + " print(s)\n", + "sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 8. Define a function to print nth term in Fibonacci series using recursion. Call this function with different values." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x: 6\n", + "8\n" + ] + } + ], + "source": [ + "def fib(n):\n", + " if(n==1):\n", + " return 1\n", + " elif(n==2):\n", + " return 1\n", + " else:\n", + " return(fib(n-1)+fib(n-2))\n", + "x=int(input('x: '))\n", + "print(fib(x))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 9. Define a python function to convert a decimal number to its equivalent octal number without using inbuilt functions. Call this function with different values." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x: 8\n", + "10\n" + ] + } + ], + "source": [ + "def dec2oct(n):\n", + " s=0\n", + " i=1\n", + " while(n!=0):\n", + " d=(n%8)*i\n", + " s=s+d\n", + " i*=10\n", + " n//=8\n", + " return s\n", + "x=int(input('x: '))\n", + "print(dec2oct(x))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 10. Write a python function to Check whether a number can be expressed as the sum of two prime number. For example, 20 can be written as sum of 7 and 13. 29 cannot be written as sum of 2 prime numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the Number: 89\n", + "It can not be Written as the sum\n" + ] + } + ], + "source": [ + "def check(a):\n", + " z=True\n", + " for i in range(2,a):\n", + " if(a%i==0):\n", + " z=False\n", + " return z\n", + "def check2(b):\n", + " for j in range(2,b):\n", + " if(check(j) and check(b-j)):\n", + " print('It can be Written as the sum')\n", + " break\n", + " elif(j==b-1):\n", + " print('It can not be Written as the sum')\n", + " \n", + "x=int(input('Enter the Number: '))\n", + "check2(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Optional Questions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 11. Write two python function to Convert octal Number to decimal without using inbuilt functions." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter n: 1000\n", + "512\n" + ] + } + ], + "source": [ + "def conv(a,n,s):\n", + " if(a<=0):\n", + " return n\n", + " else:\n", + " n+=(8**(s))*(a%10)\n", + " return conv(a//10,n,s+1)\n", + "x=int(input('Enter n: '))\n", + "print(conv(x,0,0))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 12. Write a program in Python to check whether two given strings are an anagram. \n", + "### An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "enter the string 1 qqwerty\n", + "enter the string 2 qwerty\n", + "not an anagram\n" + ] + } + ], + "source": [ + "x=input(\"enter the string 1 \")\n", + "y=input(\"enter the string 2 \")\n", + "flag=1\n", + "for i in x:\n", + " if i not in y:\n", + " flag=0\n", + " break\n", + "for i in y:\n", + " if i not in x:\n", + " flag=0\n", + " break\n", + "if flag==1 and len(x)==len(y):\n", + " print(\"anagram\")\n", + "else:\n", + " print(\"not an anagram\") " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 13. Write a python program that calculates the bill for a customer purchase in a store. in this store the item_id range from 1 to 100. These items are organised in a way that the items with item_id less than 50 are on sale (10 %) and item_id range from 50-80 are on sale (25 %) and the rest items are on the sale of 40 %.\n", + "### Your program has a function call (saleprice) that receives the itme_id, and its price, and it returns the current price after sale for current item, and it prints the number of items purchased so far.\n", + "### In your main, you should read item id and call saleprice function for each item. your program should continue to nread item_id till the end of purchase. entering 0 will indicate the end of purchase. At the end your program should print the total amount of bill. " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hellohihihi\n" + ] + } + ], + "source": [ + "qwerty" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 14.We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary day and we add it to the shortest month of the year, February. \n", + "## In the Gregorian calendar three criteria must be taken into account to identify leap years:\n", + "\n", + "### The year can be evenly divided by 4, is a leap year, unless:\n", + "### The year can be evenly divided by 100, it is NOT a leap year, unless:\n", + "### The year is also evenly divisible by 400. Then it is a leap year.\n", + "### This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.\n", + "\n", + "## Sample Input\n", + "### 1990\n", + "\n", + "## Sample Output\n", + "### False\n", + "\n", + "## Sample Input\n", + "### 1600\n", + "\n", + "## Sample Output\n", + "### True" + ] + }, + { + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}