Skip to content

Instantly share code, notes, and snippets.

@jhamrick
Created April 10, 2014 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhamrick/10344303 to your computer and use it in GitHub Desktop.
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.
{
"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