Dnomd343
2 years ago
1 changed files with 39 additions and 3 deletions
@ -1,12 +1,48 @@ |
|||
#!/usr/bin/env python3 |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
|
|||
target = [ |
|||
punctuationPairs = [ |
|||
('‘', '’'), |
|||
('“', '”'), |
|||
('《', '》'), |
|||
('(', ')'), |
|||
] |
|||
|
|||
print(target[0]) |
|||
|
|||
def isLeftPunctuation(char: str) -> bool: |
|||
for punctuationPair in punctuationPairs: |
|||
if char == punctuationPair[0]: |
|||
return True |
|||
return False |
|||
|
|||
|
|||
def isRightPunctuation(char: str) -> bool: |
|||
for punctuationPair in punctuationPairs: |
|||
if char == punctuationPair[1]: |
|||
return True |
|||
return False |
|||
|
|||
|
|||
def getLeftPunctuation(char: str) -> str: |
|||
for punctuationPair in punctuationPairs: |
|||
if char == punctuationPair[1]: |
|||
return punctuationPair[0] |
|||
return '' # no match |
|||
|
|||
|
|||
def pairsCheck(sentence: str) -> bool: |
|||
punctuationStack = [] |
|||
sentence = list(sentence) |
|||
for i in range(0, len(sentence)): |
|||
char = sentence[i] |
|||
if isLeftPunctuation(char): |
|||
punctuationStack.append(char) |
|||
elif isRightPunctuation(char): |
|||
if punctuationStack.pop() != getLeftPunctuation(char): |
|||
return False |
|||
return len(punctuationStack) == 0 |
|||
|
|||
|
|||
print( |
|||
pairsCheck('测试“这个是OK的《2333》没错‘’”嗯嗯') |
|||
) |
|||
|
Loading…
Reference in new issue