{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Advanced Functions Test \n", "\n", "### For this test, you should use the built-in functions to be able to write the requested functions in one line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 1\n", "\n", "Use map to create a function which finds the length of each word in the phrase\n", "(broken by spaces) and return the values in a list.\n", "\n", "The function will have an input of a string, and output a list of integers." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def word_lengths(phrase):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 4, 3, 3, 5, 2, 4, 6]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "word_lengths('How long are the words in this phrase')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 2 \n", "\n", "Use reduce to take a list of digits and return the number that they\n", "correspond to. *Do not convert the integers to strings!* " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def digits_to_num(digits):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "34321" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "digits_to_num([3,4,3,2,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 3\n", "\n", "Use filter to return the words from a list of words which start with a target letter." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def filter_words(word_list, letter):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['hello', 'ham', 'hi', 'heart']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['hello','are','cat','dog','ham','hi','go','to','heart']\n", "filter_words(l,'h')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 4\n", "\n", "Use zip and list comprehension to return a list of the same length where each value is the two strings from\n", "L1 and L2 concatenated together with connector between them. Look at the example output below:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def concatenate(L1, L2, connector):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['A-a', 'B-b']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "concatenate(['A','B'],['a','b'],'-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 5\n", "\n", "Use enumerate and other skills to return a dictionary which has the values of the list as keys and the index as the value. You may assume that a value will only appear once in the given list.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def d_list(L):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'a': 0, 'b': 1, 'c': 2}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d_list(['a','b','c'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Problem 6\n", "\n", "Use enumerate and other skills from above to return the count of the number of items in the list whose value equals its index.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def count_match_index(L):\n", " \n", " pass" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_match_index([0,2,2,1,5,5,6,10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Great Job!" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }