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

unix_match_part_1.translate_pattern()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 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