From 03597adc58d9c1f82bcbe4865e635e560a77475b Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 27 Jan 2015 10:24:42 -0500 Subject: [PATCH] Updated markdown discussions for clarity and consistency. --- core/structs.ipynb | 326 +++++++++++++++++++++++---------------------- 1 file changed, 164 insertions(+), 162 deletions(-) diff --git a/core/structs.ipynb b/core/structs.ipynb index 18f4a8b..c5befbb 100644 --- a/core/structs.ipynb +++ b/core/structs.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:af3d280883daa542ff4a3cccb240979e6b7c05fbe40cac48675cc8ef613e3483" + "signature": "sha256:8ee2873e1a28218e63482afb0f83c35bf5a94310ceb69ce5945a5b710720ecb8" }, "nbformat": 3, "nbformat_minor": 0, @@ -36,7 +36,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "One dimensional, fixed-length, immutable sequence" + "A tuple is a one dimensional, fixed-length, immutable sequence.\n", + "\n", + "Create a tuple:" ] }, { @@ -60,28 +62,18 @@ ], "prompt_number": 1 }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "list_1 = [1, 2, 3]" - ], - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 2 - }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Convert to a tuple" + "Convert to a tuple:" ] }, { "cell_type": "code", "collapsed": false, "input": [ + "list_1 = [1, 2, 3]\n", "type(tuple(list_1))" ], "language": "python", @@ -90,19 +82,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 3, + "prompt_number": 2, "text": [ "tuple" ] } ], - "prompt_number": 3 + "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Nested tuples" + "Create a nested tuple:" ] }, { @@ -118,19 +110,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 4, + "prompt_number": 3, "text": [ "([1, 2, 3], (4, 5))" ] } ], - "prompt_number": 4 + "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Access by index O(1)" + "Access a tuple's elements by index O(1):" ] }, { @@ -145,19 +137,21 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 5, + "prompt_number": 4, "text": [ "[1, 2, 3]" ] } ], - "prompt_number": 5 + "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Although tuples are immutable, their contents can contain mutable objects" + "Although tuples are immutable, their contents can contain mutable objects. \n", + "\n", + "Modify a tuple's contents:" ] }, { @@ -173,19 +167,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 6, + "prompt_number": 5, "text": [ "[1, 2, 3, 4]" ] } ], - "prompt_number": 6 + "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Concatenate tuples by creating a new tuple and copying objects" + "Concatenate tuples by creating a new tuple and copying objects:" ] }, { @@ -200,19 +194,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 7, + "prompt_number": 6, "text": [ "(1, 3, 2, 4, 5, 6)" ] } ], - "prompt_number": 7 + "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Multiply copies references to objects (objects themselves are not copied)" + "Multiply tuples to copy references to objects (objects themselves are not copied):" ] }, { @@ -227,19 +221,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 8, + "prompt_number": 7, "text": [ "('foo', 'bar', 'foo', 'bar')" ] } ], - "prompt_number": 8 + "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Unpack tuples" + "Unpack tuples:" ] }, { @@ -255,19 +249,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 9, + "prompt_number": 8, "text": [ "([1, 2, 3, 4], (4, 5))" ] } ], - "prompt_number": 9 + "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Unpack nested tuples" + "Unpack nested tuples:" ] }, { @@ -283,19 +277,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 10, + "prompt_number": 9, "text": [ "(1, 2, 3, 4, 4, 5)" ] } ], - "prompt_number": 10 + "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "A common use of variable unpacking is when iterating over sequences of tuples or lists" + "A common use of variable unpacking is when iterating over sequences of tuples or lists:" ] }, { @@ -319,7 +313,7 @@ ] } ], - "prompt_number": 11 + "prompt_number": 10 }, { "cell_type": "markdown", @@ -332,7 +326,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "One dimensional, variable-length, mutable sequence" + "A list is a one dimensional, variable-length, mutable sequence.\n", + "\n", + "Create a list:" ] }, { @@ -348,19 +344,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 12, + "prompt_number": 11, "text": [ "[1, 2, 3]" ] } ], - "prompt_number": 12 + "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Convert to a list" + "Convert to a list:" ] }, { @@ -375,19 +371,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 13, + "prompt_number": 12, "text": [ "list" ] } ], - "prompt_number": 13 + "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Nested list" + "Create a nested list:" ] }, { @@ -403,19 +399,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 14, + "prompt_number": 13, "text": [ "[(1, 2, 3), [4, 5]]" ] } ], - "prompt_number": 14 + "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Access by index" + "Access a list's elements by index O(1):" ] }, { @@ -430,19 +426,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 15, + "prompt_number": 14, "text": [ "[4, 5]" ] } ], - "prompt_number": 15 + "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Append an element O(1)" + "Append an element to a list O(1):" ] }, { @@ -458,19 +454,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 16, + "prompt_number": 15, "text": [ "[(1, 2, 3), [4, 5], 6]" ] } ], - "prompt_number": 16 + "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Insert an element at a specific index. Insert is expensive as it has to shift subsequent elements O(n)." + "Insert an element to a list at a specific index (note that insert is expensive as it has to shift subsequent elements O(n)):" ] }, { @@ -486,19 +482,21 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 17, + "prompt_number": 16, "text": [ "['start', (1, 2, 3), [4, 5], 6]" ] } ], - "prompt_number": 17 + "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Pop removes and returns an element from a specified index. Pop is expensive as it has to shift subsequent elements O(n). O(1) if pop is used for the last element" + "Pop is expensive as it has to shift subsequent elements O(n). The operation is O(1) if pop is used for the last element.\n", + "\n", + "Remove and return an element from a specified index:" ] }, { @@ -514,19 +512,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 18, + "prompt_number": 17, "text": [ "[(1, 2, 3), [4, 5], 6]" ] } ], - "prompt_number": 18 + "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Remove locates the first such value and removes it O(n)" + "Locates the first such value and remove it O(n):" ] }, { @@ -542,19 +540,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 19, + "prompt_number": 18, "text": [ "[[4, 5], 6]" ] } ], - "prompt_number": 19 + "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Check if a list contains a value O(n)" + "Check if a list contains a value O(n):" ] }, { @@ -569,19 +567,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 20, + "prompt_number": 19, "text": [ "True" ] } ], - "prompt_number": 20 + "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Concatenate lists by creating a new list and copying objects" + "Concatenate lists by creating a new list and copying objects:" ] }, { @@ -596,19 +594,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 21, + "prompt_number": 20, "text": [ "[1, 3, 2, 4, 5, 6]" ] } ], - "prompt_number": 21 + "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Extend a list by appending elements. Faster than concatenating lists." + "Extend a list by appending elements (faster than concatenating lists, as it does not have to create a new list):" ] }, { @@ -624,13 +622,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 22, + "prompt_number": 21, "text": [ "[[4, 5], 6, 7, 8, 9]" ] } ], - "prompt_number": 22 + "prompt_number": 21 }, { "cell_type": "markdown", @@ -643,9 +641,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Also known as a hash map or associative array. A dict is a mutable collection of key-value pairs.\n", + "A dict is also known as a hash map or associative array. A dict is a mutable collection of key-value pairs.\n", "\n", - "Big O complexities are listed as average case, with most worst case complexities being O(n)." + "Note: Big O complexities are listed as average case, with most worst case complexities being O(n).\n", + "\n", + "Create a dict:" ] }, { @@ -661,19 +661,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 23, + "prompt_number": 22, "text": [ "{'a': 'foo', 'b': [0, 1, 2, 3]}" ] } ], - "prompt_number": 23 + "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Access by index O(1)" + "Access a dict's elements by index O(1)" ] }, { @@ -688,19 +688,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 24, + "prompt_number": 23, "text": [ "[0, 1, 2, 3]" ] } ], - "prompt_number": 24 + "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Insert or set by index O(1)" + "Insert or set a dict's elements by index O(1):" ] }, { @@ -716,19 +716,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 25, + "prompt_number": 24, "text": [ "{5: 'bar', 'a': 'foo', 'b': [0, 1, 2, 3]}" ] } ], - "prompt_number": 25 + "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Check if a dict contains a key O(1)" + "Check if a dict contains a key O(1):" ] }, { @@ -743,19 +743,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 26, + "prompt_number": 25, "text": [ "True" ] } ], - "prompt_number": 26 + "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Delete a value O(1)" + "Delete a value from a dict O(1):" ] }, { @@ -772,19 +772,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 27, + "prompt_number": 26, "text": [ "{'a': 'foo', 'b': [0, 1, 2, 3]}" ] } ], - "prompt_number": 27 + "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Pop a value O(1) deletes the key and returns the value" + "Remove and return an element from a specified index O(1):" ] }, { @@ -807,13 +807,13 @@ ] } ], - "prompt_number": 28 + "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Get or pop with a default value if the key is not found. By default, get() will return None and pop() will throw an exception if the key is not found." + "Get or pop can be called with a default value if the key is not found. By default, get() will return None and pop() will throw an exception if the key is not found." ] }, { @@ -829,19 +829,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 29, + "prompt_number": 28, "text": [ "0" ] } ], - "prompt_number": 29 + "prompt_number": 28 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "setdefault() is similar to get(), but returns a default value if the key is not found" + "Return a default value if the key is not found:" ] }, { @@ -863,13 +863,13 @@ ] } ], - "prompt_number": 30 + "prompt_number": 29 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "By contrast to setdefault(), defaultdict lets you specify the default when the container is initialized, which works well if the default is appropriate for all keys." + "By contrast to setdefault(), defaultdict lets you specify the default when the container is initialized, which works well if the default is appropriate for all keys:" ] }, { @@ -890,19 +890,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 31, + "prompt_number": 30, "text": [ "defaultdict(, {'b': ['bar', 'baz'], 'f': ['foo']})" ] } ], - "prompt_number": 31 + "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "dict keys must be \"hashable\": immutable objects like scalars (int, float, string) or tuples whose objects are all immutable. Lists are mutable and therefore are not hashable, although you can convert the list portion to a tuple as a quick fix" + "dict keys must be \"hashable\", i.e. they must be immutable objects like scalars (int, float, string) or tuples whose objects are all immutable. Lists are mutable and therefore are not hashable, although you can convert the list portion to a tuple as a quick fix." ] }, { @@ -924,7 +924,7 @@ ] } ], - "prompt_number": 32 + "prompt_number": 31 }, { "cell_type": "markdown", @@ -945,13 +945,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 33, + "prompt_number": 32, "text": [ "['a', 'b', 5, 'z']" ] } ], - "prompt_number": 33 + "prompt_number": 32 }, { "cell_type": "markdown", @@ -972,19 +972,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 34, + "prompt_number": 33, "text": [ "['foo', [0, 1, 2, 3], 'bar', None]" ] } ], - "prompt_number": 34 + "prompt_number": 33 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Iterate through a dictionary's keys and values" + "Iterate through a dictionary's keys and values:" ] }, { @@ -1008,13 +1008,13 @@ ] } ], - "prompt_number": 35 + "prompt_number": 34 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Merge one dict into another" + "Merge one dict into another:" ] }, { @@ -1030,7 +1030,7 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 36, + "prompt_number": 35, "text": [ "{5: 'bar',\n", " 'a': 'foo',\n", @@ -1041,13 +1041,13 @@ ] } ], - "prompt_number": 36 + "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "A common operation is to pair up two sequences element-wise in a dict" + "Pair up two sequences element-wise in a dict:" ] }, { @@ -1063,13 +1063,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 37, + "prompt_number": 36, "text": [ "{0: 6, 1: 5, 2: 4, 3: 3, 4: 2, 5: 1, 6: 0}" ] } ], - "prompt_number": 37 + "prompt_number": 36 }, { "cell_type": "markdown", @@ -1082,7 +1082,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A set is an unordered sequence of unique elements. " + "A set is an unordered sequence of unique elements. \n", + "\n", + "Create a set:" ] }, { @@ -1098,13 +1100,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 38, + "prompt_number": 37, "text": [ "{0, 1, 2, 3, 4, 5}" ] } ], - "prompt_number": 38 + "prompt_number": 37 }, { "cell_type": "code", @@ -1119,26 +1121,26 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 39, + "prompt_number": 38, "text": [ "{1, 2, 3, 5, 8, 13}" ] } ], - "prompt_number": 39 + "prompt_number": 38 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Sets support set operations like union, intersection, difference, and symmetric difference" + "Sets support set operations like union, intersection, difference, and symmetric difference." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Union O(len(set_1) + len(set_2))" + "Union O(len(set_1) + len(set_2)):" ] }, { @@ -1153,19 +1155,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 40, + "prompt_number": 39, "text": [ "{0, 1, 2, 3, 4, 5, 8, 13}" ] } ], - "prompt_number": 40 + "prompt_number": 39 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Intersection O(min(len(set_1), len(set_2))" + "Intersection O(min(len(set_1), len(set_2)):" ] }, { @@ -1180,19 +1182,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 41, + "prompt_number": 40, "text": [ "{1, 2, 3, 5}" ] } ], - "prompt_number": 41 + "prompt_number": 40 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Difference O(len(set_1))" + "Difference O(len(set_1)):" ] }, { @@ -1207,19 +1209,19 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 42, + "prompt_number": 41, "text": [ "{0, 4}" ] } ], - "prompt_number": 42 + "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Symmetric Difference O(len(set_1))" + "Symmetric Difference O(len(set_1)):" ] }, { @@ -1234,27 +1236,54 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 43, + "prompt_number": 42, "text": [ "{0, 4, 8, 13}" ] } ], + "prompt_number": 42 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Subset O(len(set_3)):" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "set_3 = {1, 2, 3}\n", + "set_3.issubset(set_2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 43, + "text": [ + "True" + ] + } + ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Subset" + "Superset O(len(set_3)):" ] }, { "cell_type": "code", "collapsed": false, "input": [ - "set_3 = {1, 2, 3}\n", - "set_3.issubset(set_2)" + "set_2.issuperset(set_3)" ], "language": "python", "metadata": {}, @@ -1274,34 +1303,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Superset" - ] - }, - { - "cell_type": "code", - "collapsed": false, - "input": [ - "set_2.issuperset(set_3)" - ], - "language": "python", - "metadata": {}, - "outputs": [ - { - "metadata": {}, - "output_type": "pyout", - "prompt_number": 45, - "text": [ - "True" - ] - } - ], - "prompt_number": 45 - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Equal" + "Equal O(min(len(set_1), len(set_2)):" ] }, { @@ -1316,13 +1318,13 @@ { "metadata": {}, "output_type": "pyout", - "prompt_number": 46, + "prompt_number": 45, "text": [ "True" ] } ], - "prompt_number": 46 + "prompt_number": 45 } ], "metadata": {}