{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def count_parenthesis(s):\n", " c = 0\n", " for e in s:\n", " if e == '(':\n", " c += 1\n", " if e == ')':\n", " c -= 1\n", " return c\n", "\n", "def floor_position(s, f):\n", " c = 0\n", " i = 0\n", " for e in s:\n", " if e == '(':\n", " c += 1\n", " i += 1\n", " if e == ')':\n", " c -= 1\n", " i += 1\n", " if c == f:\n", " break\n", " if c != f:\n", " raise Exception('Floor not reached')\n", " return i" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "assert count_parenthesis('(())') == 0\n", "assert count_parenthesis('()()') == 0\n", "assert count_parenthesis('(((') == 3\n", "assert count_parenthesis('))(((((') == 3\n", "assert count_parenthesis('())') == -1\n", "assert count_parenthesis(')())())') == -3" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing Jupyter notebook from helper.ipynb\n" ] } ], "source": [ "import import_ipynb\n", "import helper\n", "s = helper.read_file(\"2015_1.txt\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "280" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_parenthesis(s)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "assert floor_position(')', -1) == 1\n", "assert floor_position('()())', -1) == 5" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1797" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "floor_position(s, -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9.2" }, "title": "Not Quite Lisp" }, "nbformat": 4, "nbformat_minor": 2 }