{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Function Practice Exercises\n", "\n", "Problems are arranged in increasing difficulty:\n", "* Warmup - these can be solved using basic comparisons and methods\n", "* Level 1 - these may involve if/then conditional statements and simple methods\n", "* Level 2 - these may require iterating over sequences, usually with some kind of loop\n", "* Challenging - these will take some creativity to solve" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WARMUP SECTION:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LESSER OF TWO EVENS: Write a function that returns the lesser of two given numbers *if* both numbers are even, but returns the greater if one or both numbers are odd\n", " lesser_of_two_evens(2,4) --> 2\n", " lesser_of_two_evens(2,5) --> 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "lesser_of_two_evens(2,4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "lesser_of_two_evens(2,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter\n", " animal_crackers('Levelheaded Llama') --> True\n", " animal_crackers('Crazy Kangaroo') --> False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def animal_crackers(text):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "animal_crackers('Levelheaded Llama')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "animal_crackers('Crazy Kangaroo')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 *or* if one of the integers is 20. If not, return False\n", "\n", " makes_twenty(20,10) --> True\n", " makes_twenty(12,8) --> True\n", " makes_twenty(2,3) --> False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def makes_twenty(n1,n2):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "makes_twenty(20,10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "makes_twenty(2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LEVEL 1 PROBLEMS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name\n", " \n", " old_macdonald('macdonald') --> MacDonald\n", " \n", "Note: `'macdonald'.capitalize()` returns `'Macdonald'`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def old_macdonald(name):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "old_macdonald('macdonald')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### MASTER YODA: Given a sentence, return a sentence with the words reversed\n", "\n", " master_yoda('I am home') --> 'home am I'\n", " master_yoda('We are ready') --> 'ready are We'\n", " \n", "Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:\n", "\n", " >>> \"--\".join(['a','b','c'])\n", " >>> 'a--b--c'\n", "\n", "This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:\n", "\n", " >>> \" \".join(['Hello','world'])\n", " >>> \"Hello world\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def master_yoda(text):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "master_yoda('I am home')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "master_yoda('We are ready')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200\n", "\n", " almost_there(90) --> True\n", " almost_there(104) --> True\n", " almost_there(150) --> False\n", " almost_there(209) --> True\n", " \n", "NOTE: `abs(num)` returns the absolute value of a number" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def almost_there(n):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "almost_there(104)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "almost_there(150)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "almost_there(209)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LEVEL 2 PROBLEMS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### FIND 33: \n", "\n", "Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.\n", "\n", " has_33([1, 3, 3]) → True\n", " has_33([1, 3, 1, 3]) → False\n", " has_33([3, 1, 3]) → False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def has_33(nums):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "has_33([1, 3, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check\n", "has_33([1, 3, 1, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check\n", "has_33([3, 1, 3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### PAPER DOLL: Given a string, return a string where for every character in the original there are three characters\n", " paper_doll('Hello') --> 'HHHeeellllllooo'\n", " paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def paper_doll(text):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "paper_doll('Hello')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "paper_doll('Mississippi')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### BLACKJACK: Given three integers between 1 and 11, if their sum is less than or equal to 21, return their sum. If their sum exceeds 21 *and* there's an eleven, reduce the total sum by 10. Finally, if the sum (even after adjustment) exceeds 21, return 'BUST'\n", " blackjack(5,6,7) --> 18\n", " blackjack(9,9,9) --> 'BUST'\n", " blackjack(9,9,11) --> 19" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def blackjack(a,b,c):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "blackjack(5,6,7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "blackjack(9,9,9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "blackjack(9,9,11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.\n", " \n", " summer_69([1, 3, 5]) --> 9\n", " summer_69([4, 5, 6, 7, 8, 9]) --> 9\n", " summer_69([2, 1, 6, 9, 11]) --> 14" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def summer_69(arr):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "summer_69([1, 3, 5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "summer_69([4, 5, 6, 7, 8, 9])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "summer_69([2, 1, 6, 9, 11])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CHALLENGING PROBLEMS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order\n", "\n", " spy_game([1,2,4,0,0,7,5]) --> True\n", " spy_game([1,0,2,4,0,5,7]) --> True\n", " spy_game([1,7,2,0,4,5,0]) --> False\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def spy_game(nums):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "spy_game([1,2,4,0,0,7,5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "spy_game([1,0,2,4,0,5,7])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "spy_game([1,7,2,0,4,5,0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### COUNT PRIMES: Write a function that returns the *number* of prime numbers that exist up to and including a given number\n", " count_primes(100) --> 25\n", "\n", "By convention, 0 and 1 are not prime." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def count_primes(num):\n", " pass\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Check\n", "count_primes(100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Just for fun:\n", "#### PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter\n", " print_big('a')\n", " \n", " out: * \n", " * *\n", " *****\n", " * *\n", " * *\n", "HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns.
For purposes of this exercise, it's ok if your dictionary stops at \"E\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def print_big(letter):\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "print_big('a')" ] }, { "cell_type": "markdown", "metadata": {}, "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": 2 }