Last active
March 27, 2023 13:08
-
-
Save RabeyaMuna/2b3c5a0f6aeea831d564ee121b077352 to your computer and use it in GitHub Desktop.
The permissions of a file in a Linux system are split into three sets of three permissions: read, write, and execute for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission, with 4 corresponding to read, 2 to write, and 1 to execute. Or it can be written with a string using the lette…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def octal_to_string(octal): | |
result = "" | |
value_letters = [(4,"r"),(2,"w"),(1,"x")] | |
#Iterating over each digit in octal | |
for digit in [int(n) for n in str(octal)]: | |
#Checking for each of permission values | |
for value, letter in value_letters: | |
if digit >= value: | |
result += letter | |
digit -= value | |
else: | |
result += "-" | |
return result | |
print(octal_to_string(755)) # Should be rwxr-xr-x | |
print(octal_to_string(644)) # Should be rw-r--r-- | |
print(octal_to_string(750)) # Should be rwxr-x--- | |
print(octal_to_string(600)) # Should be rw------- |
hello RabeyaMuna At the 5th line, i don't getting it ! Can you explain me ?
using for loop, each digit in the octal value is taken an a single digit. Example: for the octal value 755, 7 is taken through the for loop and evaluated against the values one by one like 7>=4 and so on
for digit in (octal):
TypeError: 'int' object is not iterable
#hence converted to str
for digit in [int(n) for n in str(octal)]:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello RabeyaMuna
At the 5th line, i don't getting it !
Can you explain me ?