ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 카카오 신입 공채 1차 코딩 테스트 문제 5. 뉴스 클러스터링(난이도: 중) (Python)
    공부 2017. 12. 4. 16:16
    반응형

    2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 5. 뉴스 클러스터링(난이도: 중)



    Java에 이어 Python으로도 작성해 봤다.



    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    import re
     
     
    def str_split_by_2(in_str):
        str_list = []
     
        for i in range(0len(in_str) - 1):
            temp_str = in_str[i:i + 2]
            pattern = re.compile('[a-zA-Z]{2}')
            if pattern.match(temp_str):
                str_list.append(temp_str)
        str_list.sort()
        return str_list
     
     
    def question5(str1, str2):
        if len(str1) < 2 or len(str1) > 1000:
            return None
        if len(str2) < 2 or len(str2) > 1000:
            return None
     
        str1_list = str_split_by_2(str1)
        str2_list = str_split_by_2(str2)
     
        intersection = []
        union = []
     
        index1 = 0
        index2 = 0
     
        # get union
        while True:
            if index1 >= len(str1_list) and index2 >= len(str2_list):
                break
     
            if index1 >= len(str1_list):
                union.append(str2_list[index2])
                index2 += 1
                continue
     
            if index2 >= len(str2_list):
                union.append(str1_list[index1])
                index1 += 1
                continue
     
            if str1_list[index1].lower() == str2_list[index2].lower():
                union.append(str1_list[index1])
                index1 += 1
                index2 += 1
            elif str1_list[index1].lower() < str2_list[index2].lower():
                union.append(str1_list[index1])
                index1 += 1
            elif str1_list[index1].lower() > str2_list[index2].lower():
                union.append(str2_list[index2])
                index2 += 1
     
        # get intersection
        index1 = 0
        index2 = 0
        while True:
            if index1 >= len(str1_list) or index2 >= len(str2_list):
                break
     
            if str1_list[index1].lower() == str2_list[index2].lower():
                intersection.append(str2_list[index1])
                index1 += 1
                index2 += 1
            elif str1_list[index1].lower() < str2_list[index2].lower():
                index1 += 1
            elif str1_list[index1].lower() > str2_list[index2].lower():
                index2 += 1
     
        if len(intersection) == 0 and len(union) == 0:
            print("intersection %d, union %d" % (len(intersection), len(union)))
            return 65536
        elif len(intersection) == 0:
            print("intersection %d, union %d" % (len(intersection), len(union)))
            return 0
        elif len(union) == 0:
            print("intersection %d, union %d" % (len(intersection), len(union)))
            return 65536
     
        return int((len(intersection) / len(union)) * 65536)
     
    cs


    반응형
Designed by Tistory.