{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 22\n", "\n", "### **Question:**\n", "\n", "> **_Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically._**\n", "\n", "> **_Suppose the following input is supplied to the program:_**\n", "\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", "```\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", "---\n", "\n", "### Hints\n", "\n", "> **_In case of input data being supplied to the question, it should be assumed to be a console input._**\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ss = input().split()\n", "word = sorted(set(ss)) # split words are stored and sorted as a set\n", "\n", "for i in word:\n", " print(\"{0}:{1}\".format(i, ss.count(i)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**OR**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ss = input().split()\n", "dict = {}\n", "for i in ss:\n", " i = dict.setdefault(\n", " i, ss.count(i)\n", " ) # setdefault() function takes key & value to set it as dictionary.\n", "\n", "dict = sorted(\n", " dict.items()\n", ") # items() function returns both key & value of dictionary as a list\n", "# and then sorted. The sort by default occurs in order of 1st -> 2nd key\n", "for i in dict:\n", " print(\"%s:%d\" % (i[0], i[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**OR**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ss = input().split()\n", "dict = {\n", " i: ss.count(i) for i in ss\n", "} # sets dictionary as i-> split word & ss.count(i) -> total occurrence of i in ss\n", "dict = sorted(\n", " dict.items()\n", ") # items() function returns both key & value of dictionary as a list\n", "# and then sorted. The sort by default occurs in order of 1st -> 2nd key\n", "for i in dict:\n", " print(\"%s:%d\" % (i[0], i[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**OR**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from collections import Counter\n", "\n", "ss = input().split()\n", "ss = Counter(ss) # returns key & frequency as a dictionary\n", "ss = sorted(ss.items()) # returns as a tuple list\n", "\n", "for i in ss:\n", " print(\"%s:%d\" % (i[0], i[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Solution by: AnjanKumarG**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pprint import pprint\n", "\n", "p = input().split()\n", "pprint({i: p.count(i) for i in p})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 23\n", "\n", "### **Question:**\n", "\n", "> **_Write a method which can calculate square value of number_**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Using the ** operator which can be written as n**p where means n^p\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = int(input())\n", "print(n ** 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 24\n", "\n", "### **Question:**\n", "\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(), raw_input()_**\n", "\n", "> **_And add document for your own function_**\n", "\n", "### Hints:\n", "\n", "\n", "The built-in document method is __doc__\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(str.__doc__)\n", "print(sorted.__doc__)\n", "\n", "\n", "def pow(n, p):\n", " \"\"\"\n", " param n: This is any integer number\n", " param p: This is power over n\n", " return: n to the power p = n^p\n", " \"\"\"\n", "\n", " return n ** p\n", "\n", "\n", "print(pow(3, 4))\n", "print(pow.__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "# Question 25\n", "\n", "### **Question:**\n", "\n", "> **_Define a class, which have a class parameter and have a same instance parameter._**\n", "\n", "---\n", "\n", "### Hints:\n", "\n", "\n", "Define an instance parameter, need add it in __init__ method.You can init an object with construct parameter or set the value later\n", "\n", "\n", "---\n", "\n", "\n", "\n", "**Solutions:**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Car name is Honda\n", "Car name is Toyota\n" ] } ], "source": [ "class Car:\n", " name = \"Car\"\n", "\n", " def __init__(self, name=None):\n", " self.name = name\n", "\n", "\n", "honda = Car(\"Honda\")\n", "print(f\"{Car.name} name is {honda.name}\")\n", "\n", "toyota = Car()\n", "toyota.name = \"Toyota\"\n", "print(f\"{Car.name} name is {toyota.name}\")" ] }, { "cell_type": "markdown", "metadata": {}, "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }