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
[프로그래머스] [1차] 뉴스 클러스터링 / Python
문제주소 :programmers.co.kr/learn/courses/30/lessons/17677 코딩테스트 연습 - [1차] 뉴스 클러스터링 뉴스 클러스터링 여러 언론사에서 쏟아지는 뉴스, 특히 속보성 뉴스를 보면 비슷비슷한 제목의 기사가
dev-note-97.tistory.com
파이썬 집합
https://blockdmask.tistory.com/451
[python] 파이썬 set (집합) 자료형 정리 및 예제
안녕하세요. BlockDMask 입니다. 오늘은 파이썬에서 집합 자료형인 set 자료형에 대해서 이야기 해보려 합니다. 집합 자료형은 다른 자료형의 중복 제거할때 사용을 하기도 하는데요. 자세한것은 예
blockdmask.tistory.com
Counter class
https://www.daleseo.com/python-collections-counter/
[파이썬] collections 모듈의 Counter 클래스 사용법
Engineering Blog by Dale Seo
www.daleseo.com
count
https://ooyoung.tistory.com/76
파이썬 count( ) 문자열의 개수, 리스트의 개수 세는 함수 (Python)
count( ) - 순서 - 1. count 함수 설명 2. 문자열에서 사용 3. 리스트에서 사용 4. 에러가 발생하는 자료형 1. count 함수 문자열 안에서 찾고 싶은 문자의 개수를 찾을 수 있다. 어떤 함수는 문자열에서만
ooyoung.tistory.com
카운트는 간단하게 셀 문자가 있을때,
카운터는 많은 데이터를 세야할때 사용하자.
이중포문 빠져나가기
https://redmuffler.tistory.com/446
python 이중 for문 빠져나가기 (break)
오늘은 이중 for문을 빠져나가는 방법을 알려드리려 합니다. 먼저 소스 코드부터 보겠습니다. 이중 반복문을 사용해서 중간에 멈추지 않으면 약 1,000,000번의 연산을 하도록 했습니다. 두번째 반
redmuffler.tistory.com
isalpha,indisit,isalnum
파이썬[Python] 알파벳/숫자인지 확인하기(isalpha, isdigit, isalnum)
최근에 문자열에서 알파벳의 빈도를 확인하는 방법에 대해서 간단하게 포스팅을 했었던 적이 있습니다. 이번에는 그와 유사하게 문자열이 알파벳인지 확인하는 방법, 또는 숫자인지 확인하는
appia.tistory.com
'알고리즘 공부 > 미분류' 카테고리의 다른 글
백준 선수과목 파이썬 (0) | 2022.01.28 |
---|---|
bfs dfs (0) | 2022.01.14 |
파이썬 deep copy (0) | 2022.01.12 |
파이썬 튜플 정렬 (0) | 2022.01.12 |
파이썬 딕셔너리 (0) | 2022.01.12 |