-
카카오 신입 공채 1차 코딩 테스트 문제 5. 뉴스 클러스터링(난이도: 중) (Python)공부 2017. 12. 4. 16:16반응형
2017/11/03 - [공부] - 카카오 신입 공채 1차 코딩 테스트 문제 5. 뉴스 클러스터링(난이도: 중)
Java에 이어 Python으로도 작성해 봤다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import redef str_split_by_2(in_str):str_list = []for i in range(0, len(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_listdef question5(str1, str2):if len(str1) < 2 or len(str1) > 1000:return Noneif len(str2) < 2 or len(str2) > 1000:return Nonestr1_list = str_split_by_2(str1)str2_list = str_split_by_2(str2)intersection = []union = []index1 = 0index2 = 0# get unionwhile True:if index1 >= len(str1_list) and index2 >= len(str2_list):breakif index1 >= len(str1_list):union.append(str2_list[index2])index2 += 1continueif index2 >= len(str2_list):union.append(str1_list[index1])index1 += 1continueif str1_list[index1].lower() == str2_list[index2].lower():union.append(str1_list[index1])index1 += 1index2 += 1elif str1_list[index1].lower() < str2_list[index2].lower():union.append(str1_list[index1])index1 += 1elif str1_list[index1].lower() > str2_list[index2].lower():union.append(str2_list[index2])index2 += 1# get intersectionindex1 = 0index2 = 0while True:if index1 >= len(str1_list) or index2 >= len(str2_list):breakif str1_list[index1].lower() == str2_list[index2].lower():intersection.append(str2_list[index1])index1 += 1index2 += 1elif str1_list[index1].lower() < str2_list[index2].lower():index1 += 1elif str1_list[index1].lower() > str2_list[index2].lower():index2 += 1if len(intersection) == 0 and len(union) == 0:print("intersection %d, union %d" % (len(intersection), len(union)))return 65536elif len(intersection) == 0:print("intersection %d, union %d" % (len(intersection), len(union)))return 0elif len(union) == 0:print("intersection %d, union %d" % (len(intersection), len(union)))return 65536return int((len(intersection) / len(union)) * 65536)cs 반응형