Passed
Push — master ( be414c...be1b15 )
by Ken M.
01:42
created

unix_match_part_1   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A unix_match() 0 2 1
A translate_pattern() 0 2 1
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