{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def load_testcase(filename):\n",
    "    with open(filename, 'r') as f:\n",
    "        lines = f.read().splitlines()\n",
    "        t = int(lines[0])\n",
    "        return (t, lines[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def flip_pancake(pancakes, i):\n",
    "    if pancakes[i] == '+':\n",
    "       pancakes[i] = '-'\n",
    "    else:\n",
    "        pancakes[i] = '+'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def flip_pancakes(pancakes, index):\n",
    "    for i in range(index, -1, -1):\n",
    "        flip_pancake(pancakes, i)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def solve_testcase(testcase):\n",
    "    tcase = []\n",
    "    for c in testcase:\n",
    "        tcase.append(c)\n",
    "    t_len = len(tcase)\n",
    "    ret = 0\n",
    "    \n",
    "    for index in range(t_len - 1, -1, -1):\n",
    "        if tcase[index] == '-':\n",
    "            ret += 1\n",
    "            flip_pancakes(tcase, index)\n",
    "    \n",
    "    return ret\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def solve_all_testcases(testcases):\n",
    "    t = testcases[0]\n",
    "    vals = []\n",
    "    for testcase in testcases[1]:\n",
    "        vals.append(solve_testcase(testcase))\n",
    "    return vals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def write_solutions_to_file(out_fileName, testcases_solutions):\n",
    "    with open(out_fileName, 'w') as f:\n",
    "        t_len = len(testcases_solutions)\n",
    "        for t in range(0, t_len):\n",
    "            f.write('Case #' + str(t + 1) + ': ' + str(testcases_solutions[t]))\n",
    "            if t != t_len-1:\n",
    "                f.write('\\n')\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "load_testcase()\n",
    "testcases = load_testcase('B-large.in')\n",
    "testcases_solutions = solve_all_testcases(testcases)\n",
    "write_solutions_to_file('B-large.out',testcases_solutions )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "testcases1 = load_testcase('rev1.in')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "s1 = solve_testcase(testcase1[1][0])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(list(range(10, -1, -1)))\n",
    "LL = list(range(10, -1, -1))\n",
    "len(LL)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
