mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Add longest abs file path challenge
This commit is contained in:
parent
0cd05ebe36
commit
d3b4b79aa7
0
online_judges/longest_abs_file_path/__init__.py
Normal file
0
online_judges/longest_abs_file_path/__init__.py
Normal file
206
online_judges/longest_abs_file_path/longest_path_challenge.ipynb
Normal file
206
online_judges/longest_abs_file_path/longest_path_challenge.ipynb
Normal file
|
@ -0,0 +1,206 @@
|
|||
{
|
||||
"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": [
|
||||
"# Challenge 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",
|
||||
"<pre>\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",
|
||||
"</pre>\n",
|
||||
"\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)\n",
|
||||
"* [Solution Notebook](#Solution-Notebook)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Solution(object):\n",
|
||||
"\n",
|
||||
" def length_longest_path(self, file_system):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %load test_length_longest_path.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSolution(object):\n",
|
||||
"\n",
|
||||
" def test_length_longest_path(self):\n",
|
||||
" solution = Solution()\n",
|
||||
" assert_raises(TypeError, solution.length_longest_path, None)\n",
|
||||
" assert_equal(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",
|
||||
" assert_equal(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": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Solution Notebook\n",
|
||||
"\n",
|
||||
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
242
online_judges/longest_abs_file_path/longest_path_solution.ipynb
Normal file
242
online_judges/longest_abs_file_path/longest_path_solution.ipynb
Normal file
|
@ -0,0 +1,242 @@
|
|||
{
|
||||
"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",
|
||||
"<pre>\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",
|
||||
"</pre>\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": {
|
||||
"collapsed": false
|
||||
},
|
||||
"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": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting test_length_longest_path.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_length_longest_path.py\n",
|
||||
"from nose.tools import assert_equal, assert_raises\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestSolution(object):\n",
|
||||
"\n",
|
||||
" def test_length_longest_path(self):\n",
|
||||
" solution = Solution()\n",
|
||||
" assert_raises(TypeError, solution.length_longest_path, None)\n",
|
||||
" assert_equal(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",
|
||||
" assert_equal(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": {
|
||||
"collapsed": false
|
||||
},
|
||||
"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.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
from nose.tools import assert_equal, assert_raises
|
||||
|
||||
|
||||
class TestSolution(object):
|
||||
|
||||
def test_length_longest_path(self):
|
||||
solution = Solution()
|
||||
assert_raises(TypeError, solution.length_longest_path, None)
|
||||
assert_equal(solution.length_longest_path(''), 0)
|
||||
file_system = 'dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext'
|
||||
expected = 32
|
||||
assert_equal(solution.length_longest_path(file_system), expected)
|
||||
print('Success: test_length_longest_path')
|
||||
|
||||
|
||||
def main():
|
||||
test = TestSolution()
|
||||
test.test_length_longest_path()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user