{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions and Methods Homework Solutions\n", "____\n", "**Write a function that computes the volume of a sphere given its radius.**" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def vol(rad):\n", " return (4.0/3)*(3.14)*(rad**3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "**Write a function that checks whether a number is in a given range (Inclusive of high and low)**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def ran_check(num,low,high):\n", " #Check if num is between low and high (including low and high)\n", " if num in range(low,high+1): \n", " print \" %s is in the range\" %str(num) \n", " else : \n", " print \"The number is outside the range.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you only wanted to return a boolean:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def ran_bool(num,low,high):\n", " return num in range(low,high+1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ran_bool(3,1,10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters.**\n", "\n", " Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", " Expected Output : \n", " No. of Upper case characters : 4\n", " No. of Lower case Characters : 33\n", "\n", "If you feel ambitious, explore the Collections module to solve this problem!" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def up_low(s):\n", " d={\"upper\":0, \"lower\":0}\n", " for c in s:\n", " if c.isupper():\n", " d[\"upper\"]+=1\n", " elif c.islower():\n", " d[\"lower\"]+=1\n", " else:\n", " pass\n", " print \"Original String : \", s\n", " print \"No. of Upper case characters : \", d[\"upper\"]\n", " print \"No. of Lower case Characters : \", d[\"lower\"]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original String : Hello Mr. Rogers, how are you this fine Tuesday?\n", "No. of Upper case characters : 4\n", "No. of Lower case Characters : 33\n" ] } ], "source": [ "s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", "up_low(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n", "\n", " Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n", " Unique List : [1, 2, 3, 4, 5]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def unique_list(l):\n", " # Also possible to use list(set())\n", " x = []\n", " for a in l:\n", " if a not in x:\n", " x.append(a)\n", " return x" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unique_list([1,1,1,1,2,2,3,3,3,3,4,5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Write a Python function to multiply all the numbers in a list.**\n", "\n", " Sample List : [1, 2, 3, -4]\n", " Expected Output : -24" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def multiply(numbers): \n", " total = 1\n", " for x in numbers:\n", " total *= x \n", " return total " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-24" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply([1,2,3,-4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Write a Python function that checks whether a passed string is palindrome or not.**\n", "\n", "Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def palindrome(s):\n", " \n", " s = s.replace(' ','') # This replaces all spaces \" \" with no space ''. (Fixes issues with strings that have spaces)\n", " return s == s[::-1] # Check through slicing" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "palindrome('nurses run')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "palindrome('abcba')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Hard**:\n", "\n", "Write a Python function to check whether a string is pangram or not.\n", "\n", " Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n", " For example : \"The quick brown fox jumps over the lazy dog\"\n", "\n", "Hint: Look at the string module" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import string\n", "\n", "def ispangram(str1, alphabet=string.ascii_lowercase): \n", " alphaset = set(alphabet) \n", " return alphaset <= set(str1.lower()) " ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ispangram(\"The quick brown fox jumps over the lazy dog\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'abcdefghijklmnopqrstuvwxyz'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.ascii_lowercase" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }