Added sort snippets

This commit is contained in:
Donne Martin 2015-01-25 08:18:02 -05:00
parent 7537b138d6
commit bf990b4beb

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:cafae0e55ae01630aff5ca46caf56896315bc7fec5174ea75bcaa1502717d3fa" "signature": "sha256:14f0a7907fbd8cdf35645b7ffb0656b900a879fbeff7073ce5c2bb91e932d356"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -19,7 +19,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Tuples" "## tuple"
] ]
}, },
{ {
@ -36,13 +36,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 48, "prompt_number": 1,
"text": [ "text": [
"(1, 2, 3)" "(1, 2, 3)"
] ]
} }
], ],
"prompt_number": 48 "prompt_number": 1
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -53,7 +53,7 @@
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 49 "prompt_number": 2
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -68,13 +68,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 50, "prompt_number": 3,
"text": [ "text": [
"tuple" "tuple"
] ]
} }
], ],
"prompt_number": 50 "prompt_number": 3
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -90,13 +90,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 51, "prompt_number": 4,
"text": [ "text": [
"([1, 2, 3], (4, 5))" "([1, 2, 3], (4, 5))"
] ]
} }
], ],
"prompt_number": 51 "prompt_number": 4
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -111,13 +111,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 52, "prompt_number": 5,
"text": [ "text": [
"[1, 2, 3]" "[1, 2, 3]"
] ]
} }
], ],
"prompt_number": 52 "prompt_number": 5
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -133,13 +133,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 53, "prompt_number": 6,
"text": [ "text": [
"[1, 2, 3, 4]" "[1, 2, 3, 4]"
] ]
} }
], ],
"prompt_number": 53 "prompt_number": 6
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -155,13 +155,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 54, "prompt_number": 7,
"text": [ "text": [
"(1, 3, 2, 4, 5, 6)" "(1, 3, 2, 4, 5, 6)"
] ]
} }
], ],
"prompt_number": 54 "prompt_number": 7
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -176,13 +176,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 55, "prompt_number": 8,
"text": [ "text": [
"('foo', 'bar', 'foo', 'bar')" "('foo', 'bar', 'foo', 'bar')"
] ]
} }
], ],
"prompt_number": 55 "prompt_number": 8
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -198,13 +198,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 56, "prompt_number": 9,
"text": [ "text": [
"([1, 2, 3, 4], (4, 5))" "([1, 2, 3, 4], (4, 5))"
] ]
} }
], ],
"prompt_number": 56 "prompt_number": 9
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -220,13 +220,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 57, "prompt_number": 10,
"text": [ "text": [
"(1, 2, 3, 4, 4, 5)" "(1, 2, 3, 4, 4, 5)"
] ]
} }
], ],
"prompt_number": 57 "prompt_number": 10
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -251,13 +251,13 @@
] ]
} }
], ],
"prompt_number": 58 "prompt_number": 11
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Lists" "## list"
] ]
}, },
{ {
@ -274,13 +274,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 59, "prompt_number": 12,
"text": [ "text": [
"[1, 2, 3]" "[1, 2, 3]"
] ]
} }
], ],
"prompt_number": 59 "prompt_number": 12
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -295,13 +295,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 60, "prompt_number": 13,
"text": [ "text": [
"list" "list"
] ]
} }
], ],
"prompt_number": 60 "prompt_number": 13
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -317,13 +317,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 61, "prompt_number": 14,
"text": [ "text": [
"[(1, 2, 3), [4, 5]]" "[(1, 2, 3), [4, 5]]"
] ]
} }
], ],
"prompt_number": 61 "prompt_number": 14
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -338,13 +338,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 62, "prompt_number": 15,
"text": [ "text": [
"[4, 5]" "[4, 5]"
] ]
} }
], ],
"prompt_number": 62 "prompt_number": 15
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -360,13 +360,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 63, "prompt_number": 16,
"text": [ "text": [
"[(1, 2, 3), [4, 5], 6]" "[(1, 2, 3), [4, 5], 6]"
] ]
} }
], ],
"prompt_number": 63 "prompt_number": 16
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -383,13 +383,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 64, "prompt_number": 17,
"text": [ "text": [
"['start', (1, 2, 3), [4, 5], 6]" "['start', (1, 2, 3), [4, 5], 6]"
] ]
} }
], ],
"prompt_number": 64 "prompt_number": 17
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -407,13 +407,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 65, "prompt_number": 18,
"text": [ "text": [
"[(1, 2, 3), [4, 5], 6]" "[(1, 2, 3), [4, 5], 6]"
] ]
} }
], ],
"prompt_number": 65 "prompt_number": 18
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -429,13 +429,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 66, "prompt_number": 19,
"text": [ "text": [
"[[4, 5], 6]" "[[4, 5], 6]"
] ]
} }
], ],
"prompt_number": 66 "prompt_number": 19
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -450,13 +450,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 67, "prompt_number": 20,
"text": [ "text": [
"True" "True"
] ]
} }
], ],
"prompt_number": 67 "prompt_number": 20
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -472,13 +472,13 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 68, "prompt_number": 21,
"text": [ "text": [
"[1, 3, 2, 4, 5, 6]" "[1, 3, 2, 4, 5, 6]"
] ]
} }
], ],
"prompt_number": 68 "prompt_number": 21
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -495,13 +495,66 @@
{ {
"metadata": {}, "metadata": {},
"output_type": "pyout", "output_type": "pyout",
"prompt_number": 71, "prompt_number": 22,
"text": [ "text": [
"[[4, 5], 6, 7, 8, 9, 7, 8, 9]" "[[4, 5], 6, 7, 8, 9]"
] ]
} }
], ],
"prompt_number": 71 "prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sort"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Sort in-place O(n log n)\n",
"a_list = [1, 5, 3, 9, 7, 6]\n",
"a_list.sort()\n",
"a_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 25,
"text": [
"[1, 3, 5, 6, 7, 9]"
]
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Sort by secondary key: str length\n",
"b_list = ['the', 'quick', 'brown', 'fox', 'jumps', 'over']\n",
"b_list.sort(key=len)\n",
"b_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"['the', 'fox', 'over', 'quick', 'brown', 'jumps']"
]
}
],
"prompt_number": 26
}, },
{ {
"cell_type": "code", "cell_type": "code",