from collections import Counter
str1 = "handshake"
str2 = "shake hands"
alphabet="abcdefghijklmnopqrstuvwxyz"
def solution(str1, str2):
answer = 0
set1 = []
set2 = []
gyu = 0
hap = 0
# 대문자를 소문자로
str1 = str1.lower()
str2 = str2.lower()
# #특수 문자 정리
# for w in str1 :
# if w not in alphabet :
# str1=str1.replace(w,"")
# for w in str2 :
# if w not in alphabet :
# str2=str2.replace(w,"")
for i in range(len(str1)-1) :
w = str1[i:i+2]
if w.isalpha() :
set1.append(str1[i:i+2])
for i in range(len(str2)-1) :
w = str2[i:i+2]
if w.isalpha() :
set2.append(str2[i:i+2])
set1_count = Counter(set1)
set2_count = Counter(set2)
hap = sum((set2_count | set1_count).values())
gyu = sum((set2_count & set1_count).values())
print(gyu)
print(hap)
if gyu == 0 and hap == 0 :
answer = 65536
print(answer)
return answer
answer = int((gyu / hap) * 65536)
print(answer)
print(f'str1: {str1}')
print(f'str2: {str2}')
print(f'set1: {set1}')
print(f'set2: {set2}')
print(f'set1_count: {set1_count}')
print(f'set2_count: {set2_count}')
print(f'set2_count & set1_count: {set2_count & set1_count}')
print(f'set2_count | set1_count: {set2_count | set1_count}')
print(f'(set2_count | set1_count).values() : {(set2_count | set1_count).values()}')
# # str1: france
# str2: french
# set1: {'an', 'nc', 'fr', 'ce', 'ra'}
# set2: {'re', 'nc', 'ch', 'fr', 'en'}
# set1_count: Counter({'an': 1, 'nc': 1, 'fr': 1, 'ce': 1, 'ra': 1})
# set2_count: Counter({'re': 1, 'nc': 1, 'ch': 1, 'fr': 1, 'en': 1})
# set2_count & set1_count: Counter({'nc': 1, 'fr': 1})
# set2_count | set1_count: Counter({'re': 1, 'nc': 1, 'ch': 1, 'fr': 1, 'en': 1, 'an': 1, 'ce': 1, 'ra': 1})
# (set2_count | set1_count).values() : dict_values([1, 1, 1, 1, 1, 1, 1, 1])
return answer
solution(str1,str2)
참고한 풀이
https://dev-note-97.tistory.com/103
파이썬 집합
https://blockdmask.tistory.com/451
Counter class
https://www.daleseo.com/python-collections-counter/
count
https://ooyoung.tistory.com/76
카운트는 간단하게 셀 문자가 있을때,
카운터는 많은 데이터를 세야할때 사용하자.
이중포문 빠져나가기
https://redmuffler.tistory.com/446
isalpha,indisit,isalnum
'알고리즘 공부 > 미분류' 카테고리의 다른 글
백준 선수과목 파이썬 (0) | 2022.01.28 |
---|---|
bfs dfs (0) | 2022.01.14 |
파이썬 deep copy (0) | 2022.01.12 |
파이썬 튜플 정렬 (0) | 2022.01.12 |
파이썬 딕셔너리 (0) | 2022.01.12 |