Skip to content

Commit d4ec1ed

Browse files
Hai Hoang Danggoswami-rahul
Hai Hoang Dang
authored andcommitted
added some path problems in new algorithms/unix/ (#344)
* Add join_with_slash.py to Unix * Add full_path.py to Unix * Add split.py to Unix
1 parent 1ac287f commit d4ec1ed

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ If you want to uninstall algorithms, it is as simple as:
328328
- [pretty_print](algorithms/tree/pretty_print.py)
329329
- [same_tree](algorithms/tree/same_tree.py)
330330
- [tree](algorithms/tree/tree.py)
331+
- [unix](algorithms/unix)
332+
- [path](algorithms/unix/path/)
333+
- [join_with_slash](algorithms/unix/path/join_with_slash.py)
334+
- [full_path](algorithms/unix/path/full_path.py)
335+
- [split](algorithms/unix/path/split.py)
331336
- [union-find](algorithms/union-find)
332337
- [count_islands](algorithms/union-find/count_islands.py)
333338

algorithms/unix/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .path.join_with_slash import *
2+
from .path.full_path import *
3+
from .path.split import *

algorithms/unix/path/full_path.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Get a full absolute path a file
3+
"""
4+
import os
5+
def full_path(file):
6+
return os.path.abspath(os.path.expanduser(file))
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Both URL and file path joins use slashes as dividers between their parts.
3+
For example:
4+
5+
path/to/dir + file --> path/to/dir/file
6+
path/to/dir/ + file --> path/to/dir/file
7+
http://algorithms.com/ + part --> http://algorithms.com/part
8+
http://algorithms.com + part --> http://algorithms/part
9+
"""
10+
import os
11+
12+
def join_with_slash(base, suffix):
13+
# Remove / trailing
14+
base = base.rstrip('/')
15+
# Remove / leading
16+
suffix = suffix.lstrip('/').rstrip()
17+
full_path = "{}/{}".format(base, suffix)
18+
return full_path

algorithms/unix/path/split.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Splitting a path into 2 parts
3+
Example:
4+
Input: https://algorithms/unix/test.py (for url)
5+
Output:
6+
part[0]: https://algorithms/unix
7+
part[1]: test.py
8+
9+
Input: algorithms/unix/test.py (for file path)
10+
Output:
11+
part[0]: algorithms/unix
12+
part[1]: test.py
13+
"""
14+
import os
15+
16+
def split(path):
17+
parts = []
18+
split_part = path.rpartition('/')
19+
# Takt the origin path without the last part
20+
parts.append(split_part[0])
21+
# Take the last element of list
22+
parts.append(split_part[2])
23+
return parts

tests/test_unix.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from algorithms.unix import (
2+
join_with_slash,
3+
full_path,
4+
split
5+
)
6+
import os
7+
import unittest
8+
class TestUnixPath(unittest.TestCase):
9+
def test_join_with_slash(self):
10+
self.assertEqual("path/to/dir/file", join_with_slash("path/to/dir/", "file"))
11+
self.assertEqual("path/to/dir/file", join_with_slash("path/to/dir", "file"))
12+
self.assertEqual("http://algorithms/part", join_with_slash("http://algorithms", "part"))
13+
self.assertEqual("http://algorithms/part", join_with_slash("http://algorithms/", "part"))
14+
15+
def test_full_path(self):
16+
file_name = "file_name"
17+
# Test full path relative
18+
expect_path = "{}/{}".format(os.getcwd(), file_name)
19+
self.assertEqual(expect_path, full_path(file_name))
20+
# Test full path with expanding user
21+
# ~/file_name
22+
expect_path = "{}/{}".format(os.path.expanduser('~'), file_name)
23+
self.assertEqual(expect_path, full_path("~/{}".format(file_name)))
24+
25+
def test_split(self):
26+
# Test url path
27+
path = "https://algorithms/unix/test.py"
28+
expect_result = split(path)
29+
self.assertEqual("https://algorithms/unix", expect_result[0])
30+
self.assertEqual("test.py", expect_result[1])
31+
# Test file path
32+
path = "algorithms/unix/test.py"
33+
expect_result = split(path)
34+
self.assertEqual("algorithms/unix", expect_result[0])
35+
self.assertEqual("test.py", expect_result[1])

0 commit comments

Comments
 (0)