From 5162c13f5bd43fccb4cc611a5d278e1a39be5a01 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 25 Jan 2015 07:24:12 -0500 Subject: [PATCH] Added snippets for list data structure. --- core/structs.ipynb | 301 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 276 insertions(+), 25 deletions(-) diff --git a/core/structs.ipynb b/core/structs.ipynb index ec3d283..ca6b793 100644 --- a/core/structs.ipynb +++ b/core/structs.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:3a81774c42cd506eb4c0d4a364178d879dabd4791d93008c2f967309d872bd37" + "signature": "sha256:cafae0e55ae01630aff5ca46caf56896315bc7fec5174ea75bcaa1502717d3fa" }, "nbformat": 3, "nbformat_minor": 0, @@ -36,31 +36,31 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 46, + "prompt_number": 48, "text": [ "(1, 2, 3)" ] } ], - "prompt_number": 46 + "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ - "list_x = [1, 2, 3]" + "a_list = [1, 2, 3]" ], "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 47 + "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "# Convert to a tuple\n", - "type(tuple(list_x))" + "type(tuple(a_list))" ], "language": "python", "metadata": {}, @@ -68,13 +68,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 48, + "prompt_number": 50, "text": [ "tuple" ] } ], - "prompt_number": 48 + "prompt_number": 50 }, { "cell_type": "code", @@ -90,19 +90,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 56, + "prompt_number": 51, "text": [ "([1, 2, 3], (4, 5))" ] } ], - "prompt_number": 56 + "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ - "# Access by index\n", + "# Access by index O(1)\n", "nested_tup[0]" ], "language": "python", @@ -111,13 +111,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 57, + "prompt_number": 52, "text": [ "[1, 2, 3]" ] } ], - "prompt_number": 57 + "prompt_number": 52 }, { "cell_type": "code", @@ -133,19 +133,20 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 58, + "prompt_number": 53, "text": [ "[1, 2, 3, 4]" ] } ], - "prompt_number": 58 + "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ - "# Concatenate tuples with +\n", + "# Concatenate tuples\n", + "# Creates a new tuple and copies objects\n", "(1, 3, 2) + (4, 5, 6)" ], "language": "python", @@ -154,13 +155,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 59, + "prompt_number": 54, "text": [ "(1, 3, 2, 4, 5, 6)" ] } ], - "prompt_number": 59 + "prompt_number": 54 }, { "cell_type": "code", @@ -175,13 +176,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 60, + "prompt_number": 55, "text": [ "('foo', 'bar', 'foo', 'bar')" ] } ], - "prompt_number": 60 + "prompt_number": 55 }, { "cell_type": "code", @@ -197,13 +198,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 66, + "prompt_number": 56, "text": [ "([1, 2, 3, 4], (4, 5))" ] } ], - "prompt_number": 66 + "prompt_number": 56 }, { "cell_type": "code", @@ -219,13 +220,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 68, + "prompt_number": 57, "text": [ "(1, 2, 3, 4, 4, 5)" ] } ], - "prompt_number": 68 + "prompt_number": 57 }, { "cell_type": "code", @@ -250,7 +251,257 @@ ] } ], - "prompt_number": 72 + "prompt_number": 58 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Lists" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# One dimensional, variable-length, mutable sequence\n", + "a_list = [1, 2, 3]\n", + "a_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 59, + "text": [ + "[1, 2, 3]" + ] + } + ], + "prompt_number": 59 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Convert to a list\n", + "type(list(tup))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 60, + "text": [ + "list" + ] + } + ], + "prompt_number": 60 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Nested list\n", + "nested_list = [(1, 2, 3), [4, 5]]\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 61, + "text": [ + "[(1, 2, 3), [4, 5]]" + ] + } + ], + "prompt_number": 61 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Access by index\n", + "nested_list[1]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 62, + "text": [ + "[4, 5]" + ] + } + ], + "prompt_number": 62 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Append an element O(1)\n", + "nested_list.append(6)\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 63, + "text": [ + "[(1, 2, 3), [4, 5], 6]" + ] + } + ], + "prompt_number": 63 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Insert an element at a specific index\n", + "# Insert is expensive as it has to shift subsequent elements O(n)\n", + "nested_list.insert(0, 'start')\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 64, + "text": [ + "['start', (1, 2, 3), [4, 5], 6]" + ] + } + ], + "prompt_number": 64 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Pop removes and returns an element from a specified index\n", + "# Pop is expensive as it has to shift subsequent elements O(n)\n", + "# O(1) if pop is used for the last element\n", + "nested_list.pop(0)\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 65, + "text": [ + "[(1, 2, 3), [4, 5], 6]" + ] + } + ], + "prompt_number": 65 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Remove locates the first such value and removes it O(n)\n", + "nested_list.remove((1, 2, 3))\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 66, + "text": [ + "[[4, 5], 6]" + ] + } + ], + "prompt_number": 66 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Check if a list contains a value O(n)\n", + "6 in nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 67, + "text": [ + "True" + ] + } + ], + "prompt_number": 67 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Concatenate lists\n", + "# Creates a new list and copies objects\n", + "[1, 3, 2] + [4, 5, 6]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 68, + "text": [ + "[1, 3, 2, 4, 5, 6]" + ] + } + ], + "prompt_number": 68 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Extend a list by appending elements\n", + "# Faster than concatenating lists\n", + "nested_list.extend([7, 8, 9])\n", + "nested_list" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 71, + "text": [ + "[[4, 5], 6, 7, 8, 9, 7, 8, 9]" + ] + } + ], + "prompt_number": 71 }, { "cell_type": "code",