{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Function Practice Exercises - Solutions\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": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def lesser_of_two_evens(a,b):\n", " if a%2 == 0 and b%2 == 0:\n", " return min(a,b)\n", " else:\n", " return max(a,b)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "lesser_of_two_evens(2,4)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "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": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def animal_crackers(text):\n", " wordlist = text.split()\n", " return wordlist[0][0] == wordlist[1][0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "animal_crackers('Levelheaded Llama')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "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": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def makes_twenty(n1,n2):\n", " return (n1+n2)==20 or n1==20 or n2==20" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "makes_twenty(20,10)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "makes_twenty(20,10)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "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": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def old_macdonald(name):\n", " if len(name) > 3:\n", " return name[:3].capitalize() + name[3:].capitalize()\n", " else:\n", " return 'Name is too short!'" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'MacDonald'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "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'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def master_yoda(text):\n", " return ' '.join(text.split()[::-1])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'home am I'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "master_yoda('I am home')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ready are We'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "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": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def almost_there(n):\n", " return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "almost_there(104)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "almost_there(150)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "almost_there(209)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# LEVEL 2 PROBLEMS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### LAUGHTER: Write a function that counts the number of times a given pattern appears in a string, *including overlap*\n", "\n", " laughter('hah','hahahah') --> 3\n", "\n", "Note that `'hahahah'.count('hah')` only returns 2." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def laughter(pattern,text):\n", " out = 0\n", " for x in range(len(text)-2):\n", " if text[x:x+len(pattern)] == pattern:\n", " out += 1\n", " return out" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "laughter('hah','hahahah')" ] }, { "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": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def paper_doll(text):\n", " result = ''\n", " for char in text:\n", " result += char * 3\n", " return result" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HHHeeellllllooo'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "paper_doll('Hello')" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'MMMiiissssssiiissssssiiippppppiii'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "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": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def blackjack(a,b,c):\n", " \n", " if sum((a,b,c)) <= 21:\n", " return sum((a,b,c))\n", " elif sum((a,b,c)) <=31 and 11 in (a,b,c):\n", " return sum((a,b,c)) - 10\n", " else:\n", " return 'BUST'" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "blackjack(5,6,7)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'BUST'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "blackjack(9,9,9)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "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": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def summer_69(arr):\n", " total = 0\n", " add = True\n", " for num in arr:\n", " while add:\n", " if num != 6:\n", " total += num\n", " break\n", " else:\n", " add = False\n", " while not add:\n", " if num != 9:\n", " break\n", " else:\n", " add = True\n", " break\n", " return total" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "summer_69([1, 3, 5])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "summer_69([4, 5, 6, 7, 8, 9])" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "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": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def spy_game(nums):\n", "\n", " code = [0,0,7,'x']\n", " \n", " for num in nums:\n", " if num == code[0]:\n", " code.pop(0) # code.remove(num) also works\n", " \n", " return len(code) == 1" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "spy_game([1,2,4,0,0,7,5])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "spy_game([1,0,2,4,0,5,7])" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "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": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def count_primes(num):\n", " primes = [2]\n", " x = 3\n", " if num < 2: # for the case of num = 0 or 1\n", " return 0\n", " while x <= num:\n", " for y in range(3,x,2): # test all odd factors up to x-1\n", " if x%y == 0:\n", " x += 2\n", " break\n", " else:\n", " primes.append(x)\n", " x += 2\n", " print(primes)\n", " return len(primes)\n", " " ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n" ] }, { "data": { "text/plain": [ "25" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check\n", "count_primes(100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "BONUS: Here's a faster version that makes use of the prime numbers we're collecting as we go!" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def count_primes2(num):\n", " primes = [2]\n", " x = 3\n", " if num < 2:\n", " return 0\n", " while x <= num:\n", " for y in primes: # use the primes list!\n", " if x%y == 0:\n", " x += 2\n", " break\n", " else:\n", " primes.append(x)\n", " x += 2\n", " print(primes)\n", " return len(primes)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n" ] }, { "data": { "text/plain": [ "25" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_primes2(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": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def print_big(letter):\n", " patterns = {1:' * ',2:' * * ',3:'* *',4:'*****',5:'**** ',6:' * ',7:' * ',8:'* * ',9:'* '}\n", " alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}\n", " for pattern in alphabet[letter.upper()]:\n", " print(patterns[pattern])" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " * \n", " * * \n", "*****\n", "* *\n", "* *\n" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }