diff --git a/51 - 60 python programming exercise( in Python 3x ).ipynb b/51 - 60 python programming exercise( in Python 3x ).ipynb new file mode 100644 index 00000000..b35f7638 --- /dev/null +++ b/51 - 60 python programming exercise( in Python 3x ).ipynb @@ -0,0 +1,565 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "51-60 python programming exercise( in Python 3x ).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyP7DCe2gAmVKY69QBrWjfQe" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "GLQnf8QS2lk0" + }, + "source": [ + "# **Question 51**\n", + "Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area.\n", + "\n", + "**Hints:Use def methodName(self) to define a method.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "X3s9thrV8iyK" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JsirS97Y8oji", + "outputId": "8a73ddda-64bb-4605-88c6-47d47e243d79", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Circle(object):\n", + " def __init__(self, r):\n", + " self.radius = r\n", + "\n", + " def area(self):\n", + " return self.radius**2*3.14\n", + "\n", + "aCircle = Circle(2)\n", + "print(aCircle.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "12.56\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3XvVjhPT89q8" + }, + "source": [ + "# **Question 52**\n", + "Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area.\n", + "\n", + "**Hints:Use def methodName(self) to define a method.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AtKMD6479KDR" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LOMijHl69M3B", + "outputId": "06598ad2-f5be-4410-9333-efdfe458ec6e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Rectangle(object):\n", + " def __init__(self, l, w):\n", + " self.length = l\n", + " self.width = w\n", + "\n", + " def area(self):\n", + " return self.length*self.width\n", + "\n", + "aRectangle = Rectangle(2,10)\n", + "print(aRectangle.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "20\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9gJiuJb7-pmL" + }, + "source": [ + "# **Question 53**\n", + "Define a class named Shape and its subclass Square. The Square class has an init function which takes a length as argument. Both classes have a area function which can print the area of the shape where Shape's area is 0 by default.\n", + "\n", + "**Hints:To override a method in super class, we can define a method with the same name in the super class.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TtEUipcc--Ix" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rQJ1HY3U_Pci", + "outputId": "f0000d2f-2763-4888-81ba-fac0e85e323c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class Shape(object):\n", + " def __init__(self):\n", + " pass\n", + "\n", + " def area(self):\n", + " return 0\n", + "\n", + "class Square(Shape):\n", + " def __init__(self, l):\n", + " Shape.__init__(self)\n", + " self.length = l\n", + "\n", + " def area(self):\n", + " return self.length*self.length\n", + "\n", + "aSquare= Square(3)\n", + "print(aSquare.area())" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "54NuSVnj_w7u" + }, + "source": [ + "# **Question 54**\n", + "Please raise a RuntimeError exception.\n", + "\n", + "**Hints:Use raise() to raise an exception.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qkrbQzYH_4a2" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8Z7ZC9rt_6nr", + "outputId": "ac3e88fb-6443-447b-e5aa-39854432c160", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 163 + } + }, + "source": [ + "raise RuntimeError('something wrong')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "error", + "ename": "RuntimeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'something wrong'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m: something wrong" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "idxWE8pZAw80" + }, + "source": [ + "# **Question 55**\n", + "Write a function to compute 5/0 and use try/except to catch the exceptions.\n", + "\n", + "**Hints:Use try/except to catch exceptions.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F0pDN8n7A-Cb" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SX4xCXVRBBbk", + "outputId": "368ef1b8-96ee-42e3-bd4d-b8f255ada392", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def throws():\n", + " return 5/0\n", + "\n", + "try:\n", + " throws()\n", + "\n", + "except ZeroDivisionError:\n", + " print(\"division by zero!\")\n", + "except Exception:\n", + " print('Caught an exception')\n", + "finally:\n", + " print('In finally block for cleanup')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "division by zero!\n", + "In finally block for cleanup\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QJ2kRbhbBXz3" + }, + "source": [ + "# **Question 56**\n", + "Define a custom exception class which takes a string message as attribute.\n", + "\n", + "**Hints:To define a custom exception, we need to define a class inherited from Exception.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XeTPaaymJ7QF" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WGwHBcSVKGCE", + "outputId": "a87f6947-b4cd-4b78-b4ff-ce9c0e784411", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "class MyError(Exception):\n", + " \"\"\"My own exception class\n", + "\n", + " Attributes:\n", + " msg -- explanation of the error\n", + " \"\"\"\n", + " \n", + " def __init__(self, msg):\n", + " self.msg = msg\n", + "\n", + "error = MyError(\"something wrong\")\n", + "print(error)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "something wrong\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u3Aw0szzKjHm" + }, + "source": [ + "# **Question 57**\n", + "Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only.\n", + "\n", + "Example: \n", + "```\n", + " Input: john@google.com\n", + "\n", + " Output: john\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use \\w to match letters.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fbr6sxLBKr12" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Zx0dsGLrKxVq", + "outputId": "f21975b8-1bf1-474f-f74f-7d9bc1c5b8eb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "emailAddress =input()\n", + "pat2 = \"(\\w+)@((\\w+\\.)+(com))\"\n", + "r2 = re.match(pat2,emailAddress)\n", + "print(r2.group(1))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "john@google.com\n", + "john\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7bstEM-3LbXt" + }, + "source": [ + "# **Question 58**\n", + "Assuming that we have some email addresses in the \"username@companyname.com\" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only.\n", + "\n", + "Example: \n", + "```\n", + " Input: john@google.com\n", + "\n", + "output: google\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use \\w to match letters.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IJ5TIrmROqbC" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7iIw5wLzOtHh", + "outputId": "f91813f2-f038-4112-87a9-b8bd9f959340", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "emailAddress =input()\n", + "pat2 = \"(\\w+)@(\\w+)\\.(com)\"\n", + "r2 = re.match(pat2,emailAddress)\n", + "print(r2.group(2))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "john@google.com\n", + "google\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YxT4f1VxRaS0" + }, + "source": [ + "# **Question 59**\n", + "Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only.\n", + "\n", + "Example:\n", + "``` \n", + " Input: 2 cats and 3 dogs.\n", + "Output: ['2', '3']\n", + "```\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.\n", + "\n", + "**Hints:Use re.findall() to find all substring using regex.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BIXQxwA4SsFi" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-qqLpoQHRxvF", + "outputId": "625cbb22-f890-45d8-b725-14b10f7e6fee", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "import re\n", + "s =input()\n", + "print(re.findall(\"\\d+\",s))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2 cats and 3 dogs\n", + "['2', '3']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LnAWYPmTSwJX" + }, + "source": [ + "# **Question 60**\n", + "Print a unicode string \"hello world\".\n", + "\n", + "**Hints:Use u'strings' format to define unicode string.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c5CpC9uXTAT2" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HcQNLJ4MyG3P", + "outputId": "951ba48c-cc3a-4dfe-f63f-2a2357675d50", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "unicodeString = u\"hello world!\"\n", + "print(unicodeString)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "hello world!\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file