관리 메뉴

솜씨좋은장씨

[Programmers] 2018 KAKAO BLIND RECRUITMENT : [1차] 뉴스 클러스트링 (Python) 본문

Programming/코딩 1일 1문제

[Programmers] 2018 KAKAO BLIND RECRUITMENT : [1차] 뉴스 클러스트링 (Python)

솜씨좋은장씨 2020. 4. 19. 21:31
728x90
반응형

1일 1문제 73일차!

오늘의 문제는 프로그래머스의 2018 KAKAO BLIND RECRUITMENT : [1차] 뉴스클러스트링 입니다.

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

첫번째 제출

import re

def getTwolengthString(temp):
    re_compile = re.compile('[a-z]{2}')
    
    re_match = re.fullmatch(re_compile, temp)
    
    return re_match



def solution(str1, str2):
    str1_list = list(str1.lower())
    str2_list = list(str2.lower())

    str1_pattern = []
    str2_pattern = []

    for i in range(len(str1_list)-1):
        temp = ''.join(str1_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str1_pattern.append(temp)

    for i in range(len(str2_list)-1):
        temp = ''.join(str2_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str2_pattern.append(temp)


    print(str1_pattern)
    print(str2_pattern)



    data_union = set(str1_pattern).union(set(str2_pattern))
    
    print(data_union)

    data_intersection = set(str1_pattern).intersection(set(str2_pattern))
    
    print(data_intersection)

    if len(data_union) != 0:
        jaccard_similarity = len(data_intersection) / len(data_union)
        answer = int(jaccard_similarity * 65536)
    else:
        answer = 65536


    
    return answer

결과

중복을 포함해야하는데 set 자료형을 사용하여 계산하니 중복값들이 포함되지 않아 중복값이 들어간 값들은

 

두번째 제출

import re
from collections import Counter

def getTwolengthString(temp):
    re_compile = re.compile('[a-z]{2}')
    
    re_match = re.fullmatch(re_compile, temp)
    
    return re_match



def solution(str1, str2):
    str1_list = list(str1.lower())
    str2_list = list(str2.lower())

    str1_pattern = []
    str2_pattern = []

    for i in range(len(str1_list)-1):
        temp = ''.join(str1_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str1_pattern.append(temp)

    for i in range(len(str2_list)-1):
        temp = ''.join(str2_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str2_pattern.append(temp)


    print(str1_pattern)
    print(str2_pattern)
    
    str1_pattern_counter = Counter(str1_pattern)
    str2_pattern_counter = Counter(str2_pattern)
    
    print(str1_pattern_counter)
    print(str2_pattern_counter)



    data_union = set(str1_pattern).union(set(str2_pattern))
    
    print(data_union)

    data_intersection = set(str1_pattern).intersection(set(str2_pattern))
    
    print(data_intersection)
    
    
    
    data_union_sum = 0
    data_intersection_sum = 0
    
    str1_key = str1_pattern_counter.keys()
    str2_key = str2_pattern_counter.keys()
    
    
    
    for uni in data_union:
        
        if uni in str1_key:
            num1 = str1_pattern_counter[uni]
        if uni in str2_key:
            num1 = max(num1, str2_pattern_counter[uni])
        
        data_union_sum = data_union_sum + num1
        
    for uni in data_intersection:
        
        if uni in str1_key:
            num1 = str1_pattern_counter[uni]
        if uni in str2_key:
            num1 = min(num1, str2_pattern_counter[uni])
        
        data_intersection_sum = data_intersection_sum + num1
    
    

    if data_union_sum != 0:
        jaccard_similarity = data_intersection_sum / data_union_sum
        answer = int(jaccard_similarity * 65536)
        print(jaccard_similarity)
    else:
        answer = 65536


    
    return answer

결과

 

세번째 제출

import re
from collections import Counter

def getTwolengthString(temp):
    re_compile = re.compile('[a-z]{2}')
    
    re_match = re.fullmatch(re_compile, temp)
    
    return re_match

def solution(str1, str2):
    str1_list = list(str1.lower())
    str2_list = list(str2.lower())

    str1_pattern = []
    str2_pattern = []

    for i in range(len(str1_list)-1):
        temp = ''.join(str1_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str1_pattern.append(temp)

    for i in range(len(str2_list)-1):
        temp = ''.join(str2_list[i:i+2])

        re_match = getTwolengthString(temp)

        if re_match != None:
            str2_pattern.append(temp)

    print(str1_pattern)
    print(str2_pattern)
    
    str1_pattern_counter = Counter(str1_pattern)
    str2_pattern_counter = Counter(str2_pattern)
    
    print(str1_pattern_counter)
    print(str2_pattern_counter)



    data_union = str1_pattern_counter | str2_pattern_counter
    
    print(data_union)

    data_intersection = str1_pattern_counter & str2_pattern_counter
    
    print(data_intersection)
    
    data_union_sum = 0
    data_intersection_sum = 0
    
    data_union_sum = sum(list(data_union.values()))
    data_intersection_sum = sum(list(data_intersection.values()))

    if data_union_sum != 0:
        jaccard_similarity = data_intersection_sum / data_union_sum
        answer = int(jaccard_similarity * 65536)
        print(jaccard_similarity)
    elif data_union_sum == 0:
        answer = 65536
    elif data_intersection_sum == 0:
        answer = 0

    return answer

결과

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments