| Total Complexity | 2 |
| Total Lines | 24 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from re import search |
||
| 2 | |||
| 3 | |||
| 4 | def translate_pattern(pattern: str) -> str: |
||
| 5 | return pattern.replace('.', '\.').replace('*', '.*').replace('?', '.') |
||
| 6 | |||
| 7 | |||
| 8 | def unix_match(filename: str, pattern: str) -> bool: |
||
| 9 | return bool(search(translate_pattern(pattern), filename)) |
||
| 10 | |||
| 11 | |||
| 12 | if __name__ == '__main__': |
||
| 13 | print("Example:") |
||
| 14 | print(unix_match('somefile.txt', '*')) |
||
| 15 | |||
| 16 | # These "asserts" are used for self-checking and not for an auto-testing |
||
| 17 | assert unix_match('somefile.txt', '*') == True |
||
| 18 | assert unix_match('other.exe', '*') == True |
||
| 19 | assert unix_match('my.exe', '*.txt') == False |
||
| 20 | assert unix_match('log1.txt', 'log?.txt') == True |
||
| 21 | assert unix_match('log12.txt', 'log?.txt') == False |
||
| 22 | assert unix_match('log12.txt', 'log??.txt') == True |
||
| 23 | print("Coding complete? Click 'Check' to earn cool rewards!") |
||
| 24 |