Last active
May 9, 2023 08:39
-
-
Save junhg0211/2512f6346e0fb7f79f163882bb61d639 to your computer and use it in GitHub Desktop.
두 Slengus 단어가 같은지 다른지 확인합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from sys import argv, stderr | |
DEBUG = True | |
COUNT = r'\d+' | |
TARGET = r'\d+' | |
INTERACTION = f'({TARGET})(\\.({COUNT}))?\\,?' | |
CIRCLE = r'O|o' | |
LINE = r'I|i' | |
PART = f'({LINE}|{CIRCLE})({INTERACTION})*' | |
WORD = f'({PART})-?+' | |
class Part: | |
NEXT_ID = 0 | |
TYPE_CIRCLE = 0 | |
TYPE_LINE = 1 | |
def __init__(self, type_: int): | |
self.id = Part.NEXT_ID | |
self.type = type_ | |
self.interactions = list() | |
Part.NEXT_ID += 1 | |
def __str__(self): | |
return f'<Part id={self.id}, type={self.type}, interactions={self.interactions}>' | |
def __repr__(self): | |
return str(self) | |
def is_equal(self, other: 'Part', self_word: 'Word', other_word: 'Word'): | |
if self.id == other.id: | |
DEBUG and print('Part Same No. 1') | |
return True | |
if self.type != other.type: | |
DEBUG and print('Part Diff No. 2') | |
return False | |
if len(self.interactions) != len(other.interactions): | |
DEBUG and print('Part Diff No. 3') | |
return False | |
for self_interaction, other_interaction in zip(sorted(self.interactions), sorted(other.interactions)): | |
if self_interaction[1] != other_interaction[1]: | |
DEBUG and print('Part Diff No. 4') | |
return False | |
self_target_part = self_word.parts.get(self_interaction[0]) | |
other_target_part = other_word.parts.get(other_interaction[0]) | |
if self_target_part.type != other_target_part.type: | |
DEBUG and print('Part Diff No. 5') | |
return False | |
if len(self_target_part.interactions) != len(other_target_part.interactions): | |
DEBUG and print('Part Diff No. 6') | |
return False | |
DEBUG and print('Part Same No. 7') | |
return True | |
def interact(self, target: int, count: int): | |
self.interactions.append((target, count)) | |
class Word: | |
@staticmethod | |
def get_from_string(string): | |
result = Word() | |
id_mapping = dict() | |
for i, raw_part in enumerate(re.finditer(PART, string)): | |
raw_part = string[raw_part.start():raw_part.end()] | |
part = Part({'O': Part.TYPE_CIRCLE, 'I': Part.TYPE_LINE}[raw_part[0].upper()]) | |
DEBUG and print(f'Part: {raw_part}, {part}') | |
result.add_part(part) | |
id_mapping[i] = part.id | |
for raw_interaction in re.findall(INTERACTION, raw_part): | |
DEBUG and print(f'Interaction: {raw_interaction}') | |
target = int(raw_interaction[0]) | |
count = int(raw_interaction[2]) if raw_interaction[2] else 1 | |
part.interact(id_mapping[target], count) | |
result.parts.get(id_mapping[target]).interact(part.id, count) | |
DEBUG and print(f'PartTarget: {target}, {result.parts.get(id_mapping[target])}') | |
DEBUG and print(f'PartResult: {part}') | |
DEBUG and print(f'Word: {result}') | |
return result | |
def __init__(self): | |
self.parts = dict() | |
def __str__(self): | |
return f'<Word parts={self.parts.values()}>' | |
def is_equal(self, other: 'Word'): | |
if len(self.parts) != len(other.parts): | |
DEBUG and print(f'Word Diff No. 1') | |
return False | |
for self_part in self.parts.values(): | |
for other_part in other.parts.values(): | |
if self_part.is_equal(other_part, self, other): | |
break | |
else: | |
DEBUG and print(f'Word Diff No. 2') | |
return False | |
return True | |
def add_part(self, part: Part): | |
self.parts[part.id] = part | |
def main(): | |
if len(argv) < 3: | |
print('Word not specified.', file=stderr) | |
exit(1) | |
word1 = Word.get_from_string(argv[1]) | |
word2 = Word.get_from_string(argv[2]) | |
if word1.is_equal(word2): | |
print('Same') | |
else: | |
print('Different') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment