      "    new_length = length + 2 * num_spaces - 1\n",
      "    for i in xrange(length-1, -1, -1):\n",
      "        if chr(string[i]) == ' ':\n",
      "            string[new_length] = '0'\n",
      "            string[new_length - 1] = '2'\n",
      "            string[new_length - 2] = '%'\n",
      "            new_length -= 3\n",
      "        else:\n",
      "            string[new_length] = string[i]\n",
      "            new_length -= 1\n",
      "\n",
 
</s> remove     "            new_length -= 1\n",
    "\n",
    "run_tests(encode_spaces)"
 </s> add     "            new_length -= 1" </s> remove   {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from nose.tools import assert_equal\n",
    "\n",
    "class Test(object):\n",
    "    def test_replace_char(self, func):\n",
    "        str0 = None\n",
    "        str1 = bytearray(' ||')\n",
    "        str2 = bytearray(' foo bar ||||||')\n",
    "        str3 = bytearray('foo')\n",
    "        func(str0, 0)\n",
    "        func(str1, 1)\n",
    "        func(str2, 9)\n",
    "        func(str3, 3)\n",
    "        assert_equal(str0, None)\n",
    "        assert_equal(str1, '%20')\n",
    "        assert_equal(str2, '%20foo%20bar%20')\n",
    "        assert_equal(str3, 'foo')\n",
    "\n",
    "def run_tests(func):\n",
    "    test = Test()\n",
    "    test.test_replace_char(func)"
   ]
  },
 </s> add  </s> remove     "* Is the string ASCII (extended)?  Or Unicode?\n",
    "    * ASCII extended, which is 256 characters\n",
 </s> add     "*Problem statements are sometimes intentionally ambiguous.  Asking clarifying questions, identifying constraints, and stating assumptions help to ensure you code the intended solution.*\n",
    "\n",
    "* Can I assume the string is ASCII?\n",
    "    * Yes\n",
    "    * Note: Unicode strings could require special handling depending on your language\n", </s> remove     "Since Python strings are immutable, we'll use a bytearray instead to exercise in-place string manipulation as you would get with a C string (which is null terminated, as seen in the diagram below).  For the python bytearray we will not use a null terminator.\n",
 </s> add     "Since Python strings are immutable, we'll use a bytearray instead to exercise in-place string manipulation as you would get with a C string (which is null terminated, as seen in the diagram below).  Python does not use a null-terminator.\n",
    "            string[new_length-1] = '2'\n",
    "            string[new_length-2] = '%'\n",
      "    new_length = length + 2 * num_spaces - 1\n",
      "    for i in xrange(length-1, -1, -1):\n",
      "        if chr(string[i]) == ' ':\n",
      "            string[new_length] = '0'\n",
    "            string[new_length-1] = '2'\n",
    "            string[new_length-2] = '%'\n",
    "            string[new_length-1] = '2'\n",
    "            string[new_length-2] = '%'\n",
      "            new_length -= 3\n",
      "        else:\n",
      "            string[new_length] = string[i]\n",
      "            new_length -= 1\n",
      "\n",
keep keep keep keep replace replace keep keep keep keep keep
 Reworked notebook: Added more detail to clarifying questions and test cases.  Reworked algorithm discussion, code, and unit test. 
arrays-strings/replace_char.ipynb