{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Solution Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Find the longest absolute file path.\n", "\n", "See the [LeetCode](https://leetcode.com/problems/longest-absolute-file-path/) problem page.\n", "\n", "
\n",
    "Suppose we abstract our file system by a string in the following manner:\n",
    "\n",
    "The string \"dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext\" represents:\n",
    "\n",
    "dir\n",
    "    subdir1\n",
    "    subdir2\n",
    "        file.ext\n",
    "The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.\n",
    "\n",
    "The string \"dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext\" represents:\n",
    "\n",
    "dir\n",
    "    subdir1\n",
    "        file1.ext\n",
    "        subsubdir1\n",
    "    subdir2\n",
    "        subsubdir2\n",
    "            file2.ext\n",
    "\n",
    "The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.\n",
    "\n",
    "We are interested in finding the longest (number of characters) absolute path to a file within our file system. For example, in the second example above, the longest absolute path is \"dir/subdir2/subsubdir2/file2.ext\", and its length is 32 (not including the double quotes).\n",
    "\n",
    "Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.\n",
    "\n",
    "Note:\n",
    "The name of a file contains at least a . and an extension.\n",
    "The name of a directory or sub-directory will not contain a ..\n",
    "Time complexity required: O(n) where n is the size of the input string.\n",
    "\n",
    "Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.\n",
    "
\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Is the input a string?\n", " * Yes\n", "* Can we assume the input is valid?\n", " * No\n", "* Will there always be a file in the input?\n", " * Yes\n", "* Is the output an int?\n", " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* None -> TypeError\n", "* '' -> 0\n", "* 'dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext' -> 32" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Algorithm\n", "\n", "We'll use a dictionary `path_len` to keep track of the current character length (values) at each depth (keys). Depth 0 will have a length of 0.\n", "\n", "Initialize `max_len` to 0.\n", "\n", "Split the input based on the newline character. Iterate on each resulting line:\n", "\n", "* Extract the `name` by excluding the tab characters\n", "* Calculate the depth length by taking into account the tab characters\n", "* If we are dealing with a file path (there is a '.' in `name`)\n", " * Calculate `path_len[depth] + len(name)` and update `max_len` if needed\n", "* Else, update `path_len[depth + 1] = path_len[depth] + len(name) + 1`\n", " * We add `+ 1` because each depth is separated by the '/' character\n", "* Return `max_len`\n", "\n", "Complexity:\n", "* Time: O(n)\n", "* Space: O(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class Solution(object):\n", "\n", " def length_longest_path(self, file_system):\n", " if file_system is None:\n", " raise TypeError('file_system cannot be None')\n", " max_len = 0\n", " path_len = {0: 0}\n", " for line in file_system.splitlines():\n", " name = line.lstrip('\\t')\n", " depth = len(line) - len(name)\n", " if '.' in name:\n", " max_len = max(max_len, path_len[depth] + len(name))\n", " else:\n", " path_len[depth + 1] = path_len[depth] + len(name) + 1\n", " return max_len" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unit Test" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_length_longest_path.py\n" ] } ], "source": [ "%%writefile test_length_longest_path.py\n", "import unittest\n", "\n", "\n", "class TestSolution(unittest.TestCase):\n", "\n", " def test_length_longest_path(self):\n", " solution = Solution()\n", " self.assertRaises(TypeError, solution.length_longest_path, None)\n", " self.assertEqual(solution.length_longest_path(''), 0)\n", " file_system = 'dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext'\n", " expected = 32\n", " self.assertEqual(solution.length_longest_path(file_system), expected)\n", " print('Success: test_length_longest_path')\n", "\n", "\n", "def main():\n", " test = TestSolution()\n", " test.test_length_longest_path()\n", "\n", "\n", "if __name__ == '__main__':\n", " main()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Success: test_length_longest_path\n" ] } ], "source": [ "%run -i test_length_longest_path.py" ] } ], "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.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }