Filter Files Without Write Access
StringsAuto-graded · 14 test casesLast updated
Tests string parsing and bitwise permission logic to identify files that lack write permissions across all user categories.
You receive a collection of file records where each record contains a permission string followed by a filename, separated by a space. The permission string follows the Unix convention with exactly 9 characters.
These 9 characters are organized into three groups of three:
- Characters 0-2: owner permissions
- Characters 3-5: group permissions
- Characters 6-8: other users' permissions
Within each group, the positions represent:
- Position 0 (of the group): read permission ('r' or '-')
- Position 1 (of the group): write permission ('w' or '-')
- Position 2 (of the group): execute permission ('x' or '-')
A hyphen ('-') indicates the absence of that permission.
Your task is to identify files where no one has write permission. This means the write permission character must be '-' for the owner (index 1), the group (index 4), and others (index 7).
Return the filenames of all such files in alphabetical order.
Constraints
- The input list contains 1 to 1000 records
- Each permission string is exactly 9 characters
- Filenames contain only alphanumeric characters
- Each record follows the format:
"<9-char-permissions> <filename>"
Examples
Example 1
Input: filter_non_writable_files(['r-xr-x-x file1', 'rw-r--r-- file2', 'r--r--r-- file3'])
Output: ['file3']
Hints
Hint 1
The write permission appears at a fixed position within each of the three permission groups.
Hint 2
You only need to check three specific character positions in the permission string.
Hint 3
Don't forget to sort the results alphabetically before returning.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The permission string has a fixed structure where write permissions always appear at indices 1, 4, and 7:
Index: 0 1 2 | 3 4 5 | 6 7 8
r w x | r w x | r w x
^ ^ ^
owner group others
To find files without write access:
- Parse each record by splitting on whitespace to extract the permission string and filename
- Check whether characters at positions 1, 4, and 7 are all hyphens
- Collect filenames that pass this test
- Sort the collected filenames alphabetically
This works because a file lacks write access if and only if none of the three user categories (owner, group, others) have the write bit set.
Why It Works
The split(maxsplit=1) method cleanly separates the 9-character permission string from the filename, even if the filename theoretically contained spaces. The three write positions are at fixed indices due to the rigid 9-character format. Checking all three positions ensures we catch files where write permission is universally denied.
Python's chained comparison a == b == c == '-' evaluates to True only when all three values equal '-', which is exactly the condition we need.
Complexity
- Time: O(n log n) where n is the number of files. Each record requires constant-time parsing and checking, but sorting dominates the overall complexity.
- Space: O(k) where k is the number of matching files stored in the result list.
Solution
from typing import List
def filter_non_writable_files(file_list: List[str]) -> List[str]:
result = []
for entry in file_list:
permissions, filename = entry.split(maxsplit=1)
if permissions[1] == permissions[4] == permissions[7] == '-':
result.append(filename)
return sorted(result)