      "    test = TestFoo()\n",
      "    test.test_foo()\n",
      "\n",
      "if __name__ == '__main__':\n",
      "    main()"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 3,
 
</s> remove     "    test = TestFoo()\n",
    "    test.test_foo()\n",
 </s> add     "    test = TestGroupOrdered()\n",
    "    test.test_group_ordered()\n", </s> remove     "class TestFoo(object):\n",
    "\n",
    "    def test_foo(self):\n",
    "        assert_equal(foo(None), None)\n",
    "        assert_equal(foo(0), 0)\n",
    "        assert_equal(foo('bar'), 'bar')\n",
    "        print('Success: test_foo')\n",
 </s> add     "    def test_group_ordered(self):\n",
    "        assert_equal(group_ordered(None), None)\n",
    "        assert_equal(group_ordered([]), [])\n",
    "        assert_equal(group_ordered([1]), [1])\n",
    "        assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n",
    "        assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n",
    "        assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n",
    "        assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", </s> remove     "def foo(val):\n",
    "    return val"
 </s> add     "def make_order_list(list_in):\n",
    "    order_list = []\n",
    "    for item in list_in:\n",
    "        if item not in order_list:\n",
    "            order_list.append(item)\n",
    "    return order_list\n",
    "\n",
    "\n",
    "def group_ordered(list_in):\n",
    "    if list_in is None:\n",
    "        return None\n",
    "    order_list = make_order_list(list_in)\n",
    "    current = 0\n",
    "    for item in order_list:\n",
    "        search = current + 1\n",
    "        while True:\n",
    "            try:\n",
    "                if list_in[search] != item:\n",
    "                    search += 1\n",
    "                else:\n",
    "                    current += 1\n",
    "                    list_in[current], list_in[search] = list_in[search], list_in[current]\n",
    "                    search += 1\n",
    "            except IndexError:\n",
    "                break\n",
    "    return list_in"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm: Ordered Dict.\n",
    "\n",
    "* Use an ordered dict to track insertion order of each key\n",
    "* Flatten list of values.\n",
    "\n",
    "Complexity:\n",
    "\n",
    "* Time: O(n)\n",
    "* Space: O(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code: Ordered Dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "\n",
    "def group_ordered(list_in):\n",
    "    result = OrderedDict()\n",
    "    for value in list_in:\n",
    "        result.setdefault(value, []).append(value)\n",
    "    return [v for group in result.values() for v in group]" </s> remove    "execution_count": 3,
 </s> add    "execution_count": 4, </s> remove    "execution_count": 2,
 </s> add    "execution_count": 3,
    "    main()\n"
      "    test = TestFoo()\n",
      "    test.test_foo()\n",
      "\n",
      "if __name__ == '__main__':\n",
    "    main()\n"
     ]
    },
    {
     "cell_type": "code",
     "execution_count": 3,
keep keep keep keep replace keep keep keep keep keep
 added both solutions w/ test 
arrays_strings/group_items/group_ordered_solution.ipynb