Skip to content

Commit 9c149a8

Browse files
bites 223
1 parent 7865286 commit 9c149a8

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
/159/README.md
5454
/142/README.md
5555
/127/README.md
56+
/223/README.md

223/permissions.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def get_octal_from_file_permission(rwx: str) -> str:
2+
"""Receive a Unix file permission and convert it to
3+
its octal representation.
4+
5+
In Unix you have user, group and other permissions,
6+
each can have read (r), write (w), and execute (x)
7+
permissions expressed by r, w and x.
8+
9+
Each has a number:
10+
r = 4
11+
w = 2
12+
x = 1
13+
14+
So this leads to the following input/ outputs examples:
15+
rw-r--r-- => 644 (user = 4 + 2, group = 4, other = 4)
16+
rwxrwxrwx => 777 (user/group/other all have 4 + 2 + 1)
17+
r-xr-xr-- => 554 (user/group = 4 + 1, other = 4)
18+
"""
19+
20+
def count(ugo: str) -> str:
21+
value = 0
22+
for k, v in enumerate(ugo):
23+
if v == "r":
24+
value += 4
25+
if v == "w":
26+
value += 2
27+
if v == "x":
28+
value += 1
29+
return str(value)
30+
# :3
31+
user = rwx[:3]
32+
group = rwx[3:6]
33+
# 6:
34+
other = rwx[6:]
35+
# :D
36+
return count(user) + count(group) + count(other)
37+

223/test_permissions.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
3+
from permissions import get_octal_from_file_permission
4+
5+
6+
@pytest.mark.parametrize("input_arg, expected", [
7+
('-----x-w-', '012'), ('-----x-wx', '013'), ('-----xr--', '014'),
8+
('-----xr-x', '015'), ('-----xrw-', '016'), ('-----xrwx', '017'),
9+
('----w--wx', '023'), ('----w-r--', '024'), ('----w-r-x', '025'),
10+
('----w-rw-', '026'), ('----w-rwx', '027'), ('----wxr--', '034'),
11+
('----wxr-x', '035'), ('----wxrw-', '036'), ('----wxrwx', '037'),
12+
('---r--r-x', '045'), ('---r--rw-', '046'), ('---r--rwx', '047'),
13+
('---r-xrw-', '056'), ('---r-xrwx', '057'), ('---rw-rwx', '067'),
14+
('--x-w--wx', '123'), ('--x-w-r--', '124'), ('--x-w-r-x', '125'),
15+
('--x-w-rw-', '126'), ('--x-w-rwx', '127'), ('--x-wxr--', '134'),
16+
('--x-wxr-x', '135'), ('--x-wxrw-', '136'), ('--x-wxrwx', '137'),
17+
('--xr--r-x', '145'), ('--xr--rw-', '146'), ('--xr--rwx', '147'),
18+
('--xr-xrw-', '156'), ('--xr-xrwx', '157'), ('--xrw-rwx', '167'),
19+
('-w--wxr--', '234'), ('-w--wxr-x', '235'), ('-w--wxrw-', '236'),
20+
('-w--wxrwx', '237'), ('-w-r--r-x', '245'), ('-w-r--rw-', '246'),
21+
('-w-r--rwx', '247'), ('-w-r-xrw-', '256'), ('-w-r-xrwx', '257'),
22+
('-w-rw-rwx', '267'), ('-wxr--r-x', '345'), ('-wxr--rw-', '346'),
23+
('-wxr--rwx', '347'), ('-wxr-xrw-', '356'), ('-wxr-xrwx', '357'),
24+
('-wxrw-rwx', '367'), ('r--r-xrw-', '456'), ('r--r-xrwx', '457'),
25+
('r--rw-rwx', '467'), ('r-xrw-rwx', '567'),
26+
])
27+
def test_octal_results_for_different_rwx_combinations(input_arg, expected):
28+
assert get_octal_from_file_permission(input_arg) == expected

0 commit comments

Comments
 (0)