| Total Complexity | 2 |
| Total Lines | 24 |
| Duplicated Lines | 45.83 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | from re import search |
||
| 2 | |||
| 3 | |||
| 4 | def translate_pattern(pattern: str) -> str: |
||
| 5 | return pattern.replace('.', '\.').replace('*', '.*').replace('?', '.').replace('[!', '[^').replace('[^]', '\[!\]') |
||
| 6 | |||
| 7 | |||
| 8 | def unix_match(filename: str, pattern: str) -> bool: |
||
| 9 | translated_pattern = translate_pattern(pattern) |
||
| 10 | return ('[]' not in translated_pattern) and bool(search(translated_pattern, filename)) |
||
| 11 | |||
| 12 | |||
| 13 | View Code Duplication | if __name__ == "__main__": |
|
|
|
|||
| 14 | print("Example:") |
||
| 15 | print(unix_match("somefile.txt", "*")) |
||
| 16 | |||
| 17 | # These "asserts" are used for self-checking and not for an auto-testing |
||
| 18 | assert unix_match("somefile.txt", "somefile.txt") == True |
||
| 19 | assert unix_match("1name.txt", "[!abc]name.txt") == True |
||
| 20 | assert unix_match("log1.txt", "log[!0].txt") == True |
||
| 21 | assert unix_match("log1.txt", "log[1234567890].txt") == True |
||
| 22 | assert unix_match("log1.txt", "log[!1].txt") == False |
||
| 23 | print("Coding complete? Click 'Check' to earn cool rewards!") |
||
| 24 |