{
 "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": 1,
   "metadata": {},
   "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": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "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": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def animal_crackers(text):\n",
    "    mylist = text.split()\n",
    "    if mylist[0][0].lower() == mylist[1][0].lower():\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "animal_crackers('Levelheaded Llama')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 31,
     "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": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def makes_twenty(n1,n2):\n",
    "    if n1 == 20 or n2 == 20 or sum((n1,n2))==20:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "makes_twenty(20,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "makes_twenty(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "makes_twenty(12,8)"
   ]
  },
  {
   "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": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def old_macdonald(name):\n",
    "    mylist = []\n",
    "    for index,value in enumerate(name):\n",
    "        if index in (0,3):\n",
    "            mylist.append(value.upper())\n",
    "        else:\n",
    "            mylist.append(value)\n",
    "            \n",
    "    return ''.join(mylist)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MacDonald'"
      ]
     },
     "execution_count": 62,
     "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'\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": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "def master_yoda(text):\n",
    "    mylist = text.split()\n",
    "    return ' '.join(mylist[::-1])\n",
    "\n",
    "# yodalist = []\n",
    "#    for word in mylist[::-1]:\n",
    "#        yodalist += word+\" \"\n",
    "#    \n",
    "#    return ''.join(yodalist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'home am I'"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "master_yoda('I am home')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ready are We'"
      ]
     },
     "execution_count": 170,
     "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": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "def almost_there(n):\n",
    "    if abs(100-n) <= 10 or abs(200-n) <= 10:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "almost_there(104)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "almost_there(150)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 151,
   "metadata": {},
   "outputs": [],
   "source": [
    "def has_33(nums):\n",
    "    #print(list(item for item in zip(nums,nums[1:])))\n",
    "    for x,y in zip(nums,nums[1:]):\n",
    "        if x == y == 3:\n",
    "            return True\n",
    "    return False\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "has_33([1, 3, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "has_33([1, 3, 1, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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": 157,
   "metadata": {},
   "outputs": [],
   "source": [
    "def paper_doll(text):\n",
    "    mylist = []\n",
    "    for letter in text:\n",
    "        mylist.append(letter*3)\n",
    "    return ''.join(mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HHHeeellllllooo'"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "paper_doll('Hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MMMiiissssssiiissssssiiippppppiii'"
      ]
     },
     "execution_count": 159,
     "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": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def blackjack(a,b,c):\n",
    "    if sum((a,b,c)) <= 21:\n",
    "        return sum((a,b,c))\n",
    "    elif (a == 11 or b == 11 or c == 11):\n",
    "        return sum((a,b,c)) - 10\n",
    "    else:\n",
    "        return 'BUST'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "blackjack(5,6,7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BUST'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "blackjack(9,9,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 21,
     "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": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "def summer_69(arr):\n",
    "    result = subtract = sixth_index = ninth_index = 0\n",
    "    for index,value in enumerate(arr):\n",
    "        if value == 6:\n",
    "            sixth_index = index\n",
    "        elif value == 9:\n",
    "            ninth_index = index\n",
    "            break\n",
    "    result = sum(arr[0:])\n",
    "    if sixth_index:\n",
    "        subtract = sum(arr[sixth_index:ninth_index+1])\n",
    "    return result - subtract\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "summer_69([1, 3, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "summer_69([4, 5, 6, 7, 8, 9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 49,
     "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": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "def spy_game(nums):\n",
    "    mylist = []\n",
    "    for num in nums:\n",
    "        if num == 0 or num == 7:\n",
    "            mylist.append(num)\n",
    "    if mylist[0] == 0 and mylist[1] == 0 and mylist[2] == 7:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "spy_game([1,2,4,0,0,7,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check\n",
    "spy_game([1,0,2,4,0,5,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 122,
     "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": 136,
   "metadata": {},
   "outputs": [],
   "source": [
    "def count_primes(num):\n",
    "    count = 0\n",
    "    prime = 0\n",
    "    for i in range(2,num):\n",
    "        j = 2\n",
    "        while j < i:\n",
    "            #print (f'j is {j}')\n",
    "            if i%j == 0:\n",
    "                count += 1\n",
    "                #print (f'count {count}')\n",
    "            j += 1\n",
    "            if count == 1:\n",
    "                prime += 1\n",
    "            count = 0               \n",
    "    return prime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "276"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "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. <br>For purposes of this exercise, it's ok if your dictionary stops at \"E\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "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])\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*****\n",
      "*    \n",
      "*****\n",
      "*    \n",
      "*****\n"
     ]
    }
   ],
   "source": [
    "print_big('e')"
   ]
  },
  {
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
