Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코딩테스트 준비
- 취업리부트코스
- 프로그래머스 이중우선순위큐
- JavaScript
- spring batch 5.0
- infcon 2024
- KPT회고
- 개발자부트캠프추천
- 단기개발자코스
- 빈 충돌
- 커스텀 헤더
- 개발자 취업
- 전략패턴 #StrategyPattern #디자인패턴
- 인프콘 2024
- 파이썬
- Spring multimodule
- jwt
- 디자인 패턴
- TiL
- jwttoken
- 1주일회고
- 99클럽
- 빈 조회 2개 이상
- @FeignClient
- 구글 OAuth login
- DesignPattern
- 프로그래머스
- Python
- 항해99
- 디자인패턴
Archives
- Today
- Total
m1ndy5's coding blog
프로그래머스 완주하지 못한 선수 with Python 본문
https://school.programmers.co.kr/learn/courses/30/lessons/42576
set으로 접근했다가 동명이인이 있다는 조건을 발견하고 dictionary로 풀었다.
from collections import defaultdict
def solution(participant, completion):
answer = ''
d = defaultdict(int)
for name in participant:
d[name] += 1
for name in completion:
d[name] -= 1
for key, value in d.items():
if value == 1:
answer = key
break
return answer
뭔가 포문을 3번 도는게 마음에 안들어서 다른 사람 코드를 확인했더니
import collections
def solution(participant, completion):
answer = collections.Counter(participant) - collections.Counter(completion)
return list(answer.keys())[0]
요런 초 심플한 코드를 발견할 수 있었다,,,
collections.Counter(배열) 이렇게 넘기면 각 원소가 몇 번씩 나오는지 저장된 객체를 얻게 된다.
>>> Counter(["hi", "hey", "hi", "hi", "hello", "hey"])
Counter({'hi': 3, 'hey': 2, 'hello': 1})
키로 값을 읽을 수도 있고 value값을 갱신할 수도 있다.
counter = Counter("hello world")
counter["o"], counter["l"]
(2, 3)
counter["l"] += 1
counter["h"] -= 1
Counter({'h': 0, 'e': 1, 'l': 4, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
또한 in을 사용하여 특정 key가 counter에 존재하는지도 파악할 수 있다.
o in counter
o not in counter
counter끼리 빼기도 된다니!
신기한 지식이었다.
'알고리즘 with python > 20240909' 카테고리의 다른 글
프로그래머스 입국심사 with Python (0) | 2024.10.10 |
---|---|
프로그래머스 최소직사각형 with Python (1) | 2024.09.13 |
프로그래머스 K번째수 with Python (0) | 2024.09.12 |
프로그래머스 같은 숫자는 싫어 with Python (0) | 2024.09.11 |
프로그래머스 폰켓몬 with Python (0) | 2024.09.09 |