{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions and Methods Homework \n", "\n", "Complete the following questions:\n", "____\n", "**Write a function that computes the volume of a sphere given its radius.**\n", "

The volume of a sphere is given as $$\\frac{4}{3} πr^3$$

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def vol(rad):\n", " pass" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33.49333333333333" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "vol(2)" ] }, { "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": 3, "metadata": {}, "outputs": [], "source": [ "def ran_check(num,low,high):\n", " pass" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 is in the range between 2 and 7\n" ] } ], "source": [ "# Check\n", "ran_check(5,2,7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you only wanted to return a boolean:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def ran_bool(num,low,high):\n", " pass" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "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 calculates 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", "HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n", "\n", "If you feel ambitious, explore the Collections module to solve this problem!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def up_low(s):\n", " pass" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "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": 9, "metadata": {}, "outputs": [], "source": [ "def unique_list(lst):\n", " pass" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 10, "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": 11, "metadata": {}, "outputs": [], "source": [ "def multiply(numbers): \n", " pass" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-24" ] }, "execution_count": 12, "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 in 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": 13, "metadata": {}, "outputs": [], "source": [ "def palindrome(s):\n", " pass" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "palindrome('helleh')" ] }, { "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": 15, "metadata": {}, "outputs": [], "source": [ "import string\n", "\n", "def ispangram(str1, alphabet=string.ascii_lowercase):\n", " pass" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ispangram(\"The quick brown fox jumps over the lazy dog\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcdefghijklmnopqrstuvwxyz'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "string.ascii_lowercase" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "#### Great Job!" ] } ], "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.2" } }, "nbformat": 4, "nbformat_minor": 1 }