Writing python code to convert linux read, write and execute octal format to the string

Question:

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 letters r, w, and x or – when the permission is not granted. For example: 640 is read/write for the owner, read for the group, and no permissions for the others; converted to a string, it would be: “rw-r—–” 755 is read/write/execute for the owner, and read/execute for group and others; converted to a string, it would be: “rwxr-xr-x” Fill in the blanks to make the code convert a permission in octal format into a string format.

def octal_to_string(octal):
  result = ""
  value_letters = [(4,"r"),(2,"w"),(1,"x")]
  # Iterate over each of the digits in octal
  for ___ in [int(n) for n in str(octal)]:
      # Check for each of the permissions values
      for value, letter in value_letters:
          if ___ >= value:
              result += ___
              ___ -= value
          else:
              ___
  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-------

Please help me to fix this problem

Asked By: Kodi Learn

||

Answers:

  • An easier way.

    def octal_to_string(octal):
        permission = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"]
        result = ""
        # Iterate over each of the digits in octal
        for ___ in [int(n) for n in str(octal)]:
            result += permission[___]
        return result
    
    print(octal_to_string(755)) 
    print(octal_to_string(644)) 
    print(octal_to_string(750)) 
    print(octal_to_string(600)) 
    

Output1

  • According to your logic.

    def octal_to_string(octal):
       result = ""
       value_letters = [(4,"r"),(2,"w"),(1,"x")]
       # Iterate over each of the digits in octal
       for ___ in [int(n) for n in str(octal)]:
          # Check for each of the permissions values
          for value, letter in value_letters:
              if ___ >= value:
                   result += letter
                   ___ -= value
              else:
                   result += "-"
       return result
    
    print(octal_to_string(755))
    print(octal_to_string(644))
    print(octal_to_string(750))
    print(octal_to_string(600))
    

Output2

def octal_to_string(octal):
 result = ""
 value_letters = [(4,"r"),(2,"w"),(1,"x")]
 # Iterate over each of the digits in octal
 for i in [int(n) for n in str(octal)]:
    # Check for each of the permissions values
    for value, letter in value_letters:
        if i >= value:
            result += letter
            i -= value
        else:
            result += '-'
 return result
Answered By: Mik
def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for y in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if y >= value:
                result += letter
                y-= value
            else:
                result+="-"
    return result

print(octal_to_string(755))

rwxr-xr-x

Answered By: Sanwal Chaudhry
def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for x in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if x >= value:
                result += letter
                x -= value
            else:
                result += "-"
    return result
Answered By: Rohan Kumara
def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for x in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if x >= value:
                result += letter
                x -= 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-------
Answered By: Vĩnh Nguyên Lâm
def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    # Iterate over each of the digits in octal
    for i in [int(n) for n in str(octal)]:
        # Check for each of the permissions values
        for value, letter in value_letters:
            if i >= value:
                result += letter
                i -= 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-------
Answered By: Rohith P R

Since you question was particular about "Fill in the blanks to ….a string format."
So here we go;

def octal_to_string(octal):
    result = ""
    value_letters = [(4,"r"),(2,"w"),(1,"x")]
    
    for num in [int(n) for n in str(octal)]:
        
        for value, letter in value_letters:
            if num >= value:       #checks if num not exceeds value of value_letters
                result += letter   #adds coherent letter to result 
                num -= value       #substracts for new value to check next permission
            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-------

In this script here,
for num in [int(n) for n in str(octal)]:
Does 4 things; n iterates for each string octal numbers, int(n) converts those str values as integers, list comprehension converts obtained integers as list and finally num iterates for every integer in the list comprehension.

And, since given value_letters are tuples thus for value, letter in value_letters: iterates for for value and letter individually.
I hope I was able to help you. 😀

Answered By: Krish
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.