From 7743e06582faad9189e22195d4830f8588bc47a8 Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 18 Oct 2020 15:22:20 +0530 Subject: [PATCH 1/2] Rename 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb to 11-20 Python challenging programming exercises(in Python 3x).ipynb Rename the Heading --- ...Python challenging programming exercises(in Python 3x).ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb => 11-20 Python challenging programming exercises(in Python 3x).ipynb (99%) diff --git a/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb b/11-20 Python challenging programming exercises(in Python 3x).ipynb similarity index 99% rename from 11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb rename to 11-20 Python challenging programming exercises(in Python 3x).ipynb index 3c0a58b6..a9ce8f29 100644 --- a/11_20_Python_challenging_programming_exercises_(_in_Python_3_x).ipynb +++ b/11-20 Python challenging programming exercises(in Python 3x).ipynb @@ -711,4 +711,4 @@ ] } ] -} \ No newline at end of file +} From abbf9cbc7af8dcec8b5a25e1dc739fd8e29cfe7d Mon Sep 17 00:00:00 2001 From: Abhay kumar Date: Sun, 18 Oct 2020 16:57:53 +0530 Subject: [PATCH 2/2] Added 21-30 Question in Python 3.x --- ... programming exercises(in Python 3x).ipynb | 660 ++++++++++++++++++ 1 file changed, 660 insertions(+) create mode 100644 21-30 Python challenging programming exercises(in Python 3x).ipynb diff --git a/21-30 Python challenging programming exercises(in Python 3x).ipynb b/21-30 Python challenging programming exercises(in Python 3x).ipynb new file mode 100644 index 00000000..f3fbeb52 --- /dev/null +++ b/21-30 Python challenging programming exercises(in Python 3x).ipynb @@ -0,0 +1,660 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "21-30-Python-challenging-programming-exercises-(in-Python-3x).ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyOAW3NiHeCk497PQ5h6R1qy", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "I0tWsfOcEsyZ" + }, + "source": [ + "# **Question 21:**\n", + "A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:\n", + "\n", + "UP 5\n", + "\n", + "DOWN 3\n", + "\n", + "LEFT 3\n", + "\n", + "RIGHT 2\n", + "­\n", + "\n", + "The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.\n", + "Example:\n", + "\n", + "If the following tuples are given as input to the program:\n", + "```\n", + "UP 5\n", + "DOWN 3\n", + "LEFT 3\n", + "RIGHT 2\n", + "```\n", + "Then, the output of the program should be:\n", + "```\n", + "2\n", + "```\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rs7Ul7dOFR6N" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1INYFzJwEyUe", + "outputId": "f4e6b0d2-f050-474e-f2ab-04cd94c83bac", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + } + }, + "source": [ + "import math\n", + "pos = [0,0]\n", + "while True:\n", + " s = input()\n", + " if not s:\n", + " break\n", + " movement = s.split(\" \")\n", + " direction = movement[0]\n", + " steps = int(movement[1])\n", + " if direction==\"UP\":\n", + " pos[0]+=steps\n", + " elif direction==\"DOWN\":\n", + " pos[0]-=steps\n", + " elif direction==\"LEFT\":\n", + " pos[1]-=steps\n", + " elif direction==\"RIGHT\":\n", + " pos[1]+=steps\n", + " else:\n", + " pass\n", + "\n", + "print(int(round(math.sqrt(pos[1]**2+pos[0]**2))))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "UP 5\n", + "DOWN 3\n", + "LEFT 3\n", + "RIGHT 2\n", + "\n", + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dG8u5sHuGt5M" + }, + "source": [ + "# **Question 22:**\n", + "Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. \n", + "Suppose the following input is supplied to the program:\n", + "```\n", + "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.\n", + "```\n", + "\n", + "Then, the output should be:\n", + "```\n", + "2:2\n", + "3.:1\n", + "3?:1\n", + "New:1\n", + "Python:5\n", + "Read:1\n", + "and:1\n", + "between:1\n", + "choosing:1\n", + "or:2\n", + "to:1\n", + "```\n", + "\n", + "**Hints:\n", + "In case of input data being supplied to the question, it should be assumed to be a console input.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T7pmEajLGy8w" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "m5HFsTufGxDp", + "outputId": "dc3aa886-6b29-48cb-c33c-7c2199976be3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + } + }, + "source": [ + "freq = {} # frequency of words in text\n", + "line = input()\n", + "for word in line.split():\n", + " freq[word] = freq.get(word,0)+1\n", + "\n", + "words = freq.keys()\n", + "sorted(words)\n", + "\n", + "for w in words:\n", + " print(\"%s:%d\" % (w,freq[w]))" + ], + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "text": [ + "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3\n", + "New:1\n", + "to:1\n", + "Python:5\n", + "or:2\n", + "choosing:1\n", + "between:1\n", + "2:2\n", + "and:1\n", + "3?:1\n", + "Read:1\n", + "3:1\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wXIZyTkFJR_a" + }, + "source": [ + "# **Question 23:**\n", + " \n", + " Write a method which can calculate square value of number\n", + "\n", + "**Hints:\n", + " Using the ** operator**\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KEfhxVC0KuhT" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "j8H0zdLAKs8J", + "outputId": "5c877d6e-f39e-45c2-9599-2047eca5c706", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def square(num):\n", + " return num ** 2\n", + "\n", + "print(square(2))\n", + "print(square(3))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "4\n", + "9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "j7IBphXBLme_" + }, + "source": [ + "# **Question 24:**\n", + " Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.\n", + " \n", + "Please write a program to print some Python built-in functions documents, such as abs(), int(),input()\n", + "And add document for your own function\n", + "\n", + "**Hints:\n", + "The built-in document method is** ``` __doc__ ```" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sLYh82fMO_J6" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xp2fKTFvO7L9", + "outputId": "9947bf58-21fb-4f41-dd2b-a355e3c62d19", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 459 + } + }, + "source": [ + "print(abs.__doc__)\n", + "print(int.__doc__)\n", + "print(input.__doc__)\n", + "def square(num):\n", + "\n", + " '''\n", + "Return the square value of the input number.\n", + "The input number must be integer.\n", + "'''\n", + " return num ** 2\n", + "\n", + "print(square(2))\n", + "print(square.__doc__)" + ], + "execution_count": 44, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Return the absolute value of the argument.\n", + "int(x=0) -> integer\n", + "int(x, base=10) -> integer\n", + "\n", + "Convert a number or string to an integer, or return 0 if no arguments\n", + "are given. If x is a number, return x.__int__(). For floating point\n", + "numbers, this truncates towards zero.\n", + "\n", + "If x is not a number or if base is given, then x must be a string,\n", + "bytes, or bytearray instance representing an integer literal in the\n", + "given base. The literal can be preceded by '+' or '-' and be surrounded\n", + "by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n", + "Base 0 means to interpret the base from the string as an integer literal.\n", + ">>> int('0b100', base=0)\n", + "4\n", + "Forward raw_input to frontends\n", + "\n", + " Raises\n", + " ------\n", + " StdinNotImplentedError if active frontend doesn't support stdin.\n", + " \n", + "4\n", + "\n", + "Return the square value of the input number.\n", + "The input number must be integer.\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3WFnXSXxTdiB" + }, + "source": [ + "# **Question 25:**\n", + "Define a class, which have a class parameter and have a same instance parameter.\n", + "\n", + "**Hints:\n", + "Define a instance parameter, need add it in ```__init__``` method.You can init a object with construct parameter or set the value later.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HxThJu-1UKMp" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9IAe-wIwT7h6", + "outputId": "8a8a59c7-bf98-483a-c680-367780d14f22", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "class Person:\n", + " # Define the class parameter \"name\"\n", + " name = \"Person\"\n", + " \n", + " def __init__(self, name = None):\n", + " # self.name is the instance parameter\n", + " self.name = name\n", + "\n", + "jeffrey = Person(\"Jeffrey\")\n", + "print(\"%s name is %s\" % (Person.name, jeffrey.name))\n", + "\n", + "nico = Person()\n", + "nico.name = \"Nico\"\n", + "print(\"%s name is %s\" % (Person.name, nico.name))" + ], + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Person name is Jeffrey\n", + "Person name is Nico\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "djLbfpylUSDs" + }, + "source": [ + "# **Question 26:**\n", + "Define a function which can compute the sum of two numbers.\n", + "\n", + "**Hints:\n", + "Define a function with two numbers as arguments. You can compute the sum in the function and return the value.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ohhD2HPRUmGe" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "O-vPrYdZUjHL", + "outputId": "6d41be4a-84ae-4e4c-d6b1-890aadcfbae5", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "def SumFunction(number1, number2):\n", + "\treturn number1+number2\n", + "print(SumFunction(1,2))" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4-4tEvgZU7HI" + }, + "source": [ + "# **Question 27:**\n", + "Define a function that can convert a integer into a string and print it in console.\n", + "\n", + "**Hints:\n", + "Use str() to convert a number to string.**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wm_in-UrVHD1" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cf4B5Y1EVCKw", + "outputId": "6119c1ee-9485-49c6-80e4-9285ca71162d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(n):\n", + "\tprint(str(n))\n", + "print(printValue(3))" + ], + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "U9_tXcSrV9QG" + }, + "source": [ + "# **Question 28:**\n", + "Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.\n", + "\n", + "**Hints:\n", + "Use int() to convert a string to integer.**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Fl4lfv8RWF3B" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tznUFAF2WMdM", + "outputId": "858c5bbe-4d7a-41e6-bdf6-28ebe8ca5314", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tprint(int(s1)+int(s2))\n", + "print(printValue(\"3\",\"4\"))" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "7\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z6CdCNdPWjRG" + }, + "source": [ + "# **Question 29:**\n", + "Define a function that can accept two strings as input and concatenate them and then print it in console.\n", + "\n", + "**Hints:\n", + "Use + to concatenate the strings**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mebHXsiTWyIE" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zGi49t9SW3zL", + "outputId": "2a362e90-49ab-4be3-de10-f142cfc38c09", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tprint(s1+s2)\n", + "\n", + "print(printValue(\"3\",\"4\")) " + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34\n", + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "28d2DMkMXcTS" + }, + "source": [ + "# **Question 30:**\n", + "Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.\n", + "\n", + "**Hints:\n", + "Use len() function to get the length of a string**\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gsIr6NPgYUhI" + }, + "source": [ + "# **Solution:**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MvEa3LwZXqcZ", + "outputId": "9f3cffea-def9-4c8a-fbb6-b11acee7611b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "def printValue(s1,s2):\n", + "\tlen1 = len(s1)\n", + "\tlen2 = len(s2)\n", + "\tif len1>len2:\n", + "\t\tprint(s1)\n", + "\telif len2>len1:\n", + "\t\tprint(s2)\n", + "\telse:\n", + "\t\tprint(s1)\n", + "\t\tprint(s2)\n", + "print(printValue(\"one\",\"three\"))" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "three\n", + "None\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file