Created
April 10, 2014 05:10
-
-
Save jhamrick/10344303 to your computer and use it in GitHub Desktop.
This is a proof-of-concept for writing Python koans (https://github.com/gregmalcolm/python_koans) in an IPython notebook, based on the original about_strings.py (https://github.com/gregmalcolm/python_koans/blob/master/python2/koans/about_strings.py) koan.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:68dde4e98d73981470a2678c04c4a291b230afea11aa551a38bac92ec836e42e" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Python Koans: About Strings" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This is a proof-of-concept for writing [Python koans](https://github.com/gregmalcolm/python_koans) in an IPython notebook, based on the original [about strings](https://github.com/gregmalcolm/python_koans/blob/master/python2/koans/about_strings.py) koan." | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Varieties of quote characters" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string = \"Hello, world.\"\n", | |
"assert type(string) __ str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = 'Goodbye, world.'\n", | |
"string2 = \"Goodbye, world.\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print \"Goodbye, world.'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print 'Goodbye, world.\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"\"\"Howdy, world!\"\"\"\n", | |
"string2 = 'Howdy, world!'\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print \"\"\"Howdy, world!\"\"\"\n", | |
"print \"\"Howdy, world!\"\"\n", | |
"print \"Howdy, world!\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string = '''Bonjour tout le monde!'''\n", | |
"string = \"Bonjour tout le monde!\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print '''Bonjour tout le monde!'''\n", | |
"print ''Bonjour tout le monde!''\n", | |
"print 'Bonjour tout le monde!'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print '''Bonjour tout le monde!\"\"\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string = r\"Konnichi wa, world!\"\n", | |
"string = \"Konnichi wa, world!\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Escaping quote characters" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"a = \"He said, \\\"Don't\\\"\"\n", | |
"b = 'He said, \"Don\\'t\"'\n", | |
"assert a __ b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"a = \"He said, \\\"Don't\\\"\"\n", | |
"b = 'He said, \\'Don\"t\\''\n", | |
"assert a __ b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"a = \"He said, 'Don\\\"t'\"\n", | |
"b = 'He said, \"Don\\'t\"'\n", | |
"assert a __ b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print \"He said, \"Go Away.\"\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print 'He said, 'Go Away.''" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error\n", | |
"print 'He said, \"Don't\"'" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"a = \"Hello \\\"world\\\".\"\n", | |
"b = \"\"\"Hello \"world\".\"\"\"\n", | |
"assert a __ b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix the syntax error (hint: escape one of the quotes)\n", | |
"print \"\"\"Hello \"world\"\"\"\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Escape characters and newlines" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with the length of the string\n", | |
"string = \"\\n\"\n", | |
"assert '\\n' == string\n", | |
"assert \"\"\"\\n\"\"\" == string\n", | |
"assert __ == len(string)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with the length of the string\n", | |
"string = \"\\t\"\n", | |
"assert '\\t' == string\n", | |
"assert \"\"\"\\t\"\"\" == string\n", | |
"assert __ == len(string)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with the length of the string\n", | |
"string = \"\\m\"\n", | |
"assert '\\m' == string\n", | |
"assert \"\"\"\\m\"\"\" == string\n", | |
"assert __ == len(string)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"It was the best of times,\\n\\\n", | |
"It was the worst of times.\"\n", | |
"string2 = \"\"\"It was the best of times,\n", | |
"It was the worst of times.\"\"\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"It was the best of times,\\n\\\n", | |
"It was the worst of times.\"\n", | |
"string2 = \"It was the best of times, It was the worst of times.\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fix string1 or string2 so the test passes\n", | |
"string1 = \"It was the best of times,\\\n", | |
"It was the worst of times.\"\n", | |
"string2 = \"It was the best of times, It was the worst of times.\"\n", | |
"assert string1 == string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"It was the best of times,\\n\\\n", | |
"It was the worst of times.\"\n", | |
"string2 = \"\"\"It was the best of times,\\n\\\n", | |
"It was the worst of times.\"\"\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"\\nHowdy,\\nworld!\\n\"\n", | |
"string2 = \"\"\"\n", | |
"Howdy,\n", | |
"world!\n", | |
"\"\"\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"\\n Howdy,\\n world!\\n\"\n", | |
"string2 = \"\"\"\n", | |
"Howdy,\n", | |
"world!\n", | |
"\"\"\"\n", | |
"assert string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace the __ with an equality operator (like == or !=)\n", | |
"string1 = \"\\n\\\n", | |
"Howdy,\\n\\\n", | |
"world!\\n\\\n", | |
"\"\n", | |
"string2 = \"\"\"\n", | |
"Howdy,\n", | |
"world!\n", | |
"\"\"\"\n", | |
"string1 __ string2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 2, | |
"metadata": {}, | |
"source": [ | |
"Concatenating strings" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with a string\n", | |
"string = \"Hello, \" + \"world\"\n", | |
"assert __ == string" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with a string\n", | |
"string = \"Hallo\" \", \" \"Welt\"\n", | |
"assert __ == string" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with strings\n", | |
"hi = \"Hello, \"\n", | |
"there = \"world\"\n", | |
"string = hi + there\n", | |
"assert __ == hi\n", | |
"assert __ == there\n", | |
"assert __ == string" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with strings\n", | |
"hi = \"Hello, \"\n", | |
"there = \"world\"\n", | |
"hi += there\n", | |
"assert __ == hi\n", | |
"assert __ == there" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace __ with strings\n", | |
"original = \"Hello, \"\n", | |
"hi = original\n", | |
"there = \"world\"\n", | |
"hi += there\n", | |
"assert __ == original\n", | |
"assert __ == hi\n", | |
"assert __ == there" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment