일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
- 파이썬
- 프로그래머스 파이썬
- 편스토랑
- programmers
- 데이콘
- 편스토랑 우승상품
- Docker
- Kaggle
- AI 경진대회
- 맥북
- SW Expert Academy
- 금융문자분석경진대회
- 캐치카페
- ubuntu
- 자연어처리
- leetcode
- 백준
- 코로나19
- 프로그래머스
- Git
- ChatGPT
- 더현대서울 맛집
- Real or Not? NLP with Disaster Tweets
- dacon
- github
- gs25
- PYTHON
- Baekjoon
- hackerrank
- 우분투
- Today
- Total
솜씨좋은장씨
[BaekJoon] 11235번 : Polling (Python) 본문
코딩 1일 1문제! 오늘의 문제는 백준의 Polling 입니다.
11235번: Polling
Output the name of the candidate with the most votes. If there is a tie, output out all of the names of candidates with the most votes, one per line, in alphabetical order. Do not output any spaces, and do not output blank lines between names.
www.acmicpc.net
🧑🏻💻 문제 풀이
중간선거 결과를 입력 받으면!
가장 많은 득표를 한 사람의 이름을 오름차순으로 정렬하여 출력하는 문제입니다.
def count_election_result(election_result):
count_dict = {}
for vote in election_result:
if vote not in count_dict:
count_dict[vote] = 0
count_dict[vote] += 1
return count_dict
먼저 이름을 카운트하는 함수를 하나 만들어주었습니다.
def get_max_vote_result(count_dict):
items = sorted(count_dict.items(), key=lambda x: (-x[1], x[0]))
max_vote_num = items[0][1]
return [item[0] for item in items if max_vote_num == item[1]]
카운트하는 함수로 만든 카운트 정보를 담은 딕셔너리를 입력 받아
가장 많은 득표를 받은 사람을 구하는 함수를 만들었습니다.
items = sorted(count_dict.items(), key=lambda x: (-x[1], x[0]))
먼저 카운트 정보를 정렬합니다.
정렬기준은 카운트 수 내림차순, 이름 오름차순 입니다.
max_vote_num = items[0][1]
여기서 가장 첫번째 카운트 수를 꺼내면 가장 많이 받은 득표수 입니다.
[item[0] for item in items if max_vote_num == item[1]]
아이템 리스트에서 하나씩 꺼내면서 최대 득표수와 동일한 득표수를 받은 사람의 리스트를 구합니다.
def polling(midterm_election_result_list):
count_dict = count_election_result(
election_result=midterm_election_result_list
)
return get_max_vote_result(
count_dict=count_dict
)
위의 두 함수를 활용하여 이 문제의 솔루션 함수를 만들어줍니다.
def print_answer(answer):
for ans in answer:
print(ans)
솔루션 함수를 통해 나온 결과를 출력하는 함수를 만들어줍니다.
전체 코드는 아래를 참고해주세요.
🧑🏻💻 코드 ( Solution )
def count_election_result(election_result):
count_dict = {}
for vote in election_result:
if vote not in count_dict:
count_dict[vote] = 0
count_dict[vote] += 1
return count_dict
def get_max_vote_result(count_dict):
items = sorted(count_dict.items(), key=lambda x: (-x[1], x[0]))
max_vote_num = items[0][1]
return [item[0] for item in items if max_vote_num == item[1]]
def polling(midterm_election_result_list):
count_dict = count_election_result(
election_result=midterm_election_result_list
)
return get_max_vote_result(
count_dict=count_dict
)
def print_answer(answer):
for ans in answer:
print(ans)
if __name__ == "__main__":
election_result = []
for _ in range(int(input())):
name = input()
election_result.append(name)
answer = polling(
midterm_election_result_list=election_result
)
print_answer(
answer=answer
)
GitHub - SOMJANG/CODINGTEST_PRACTICE: 1일 1문제 since 2020.02.07
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaekJoon] 17206번 : 준석이의 수학 숙제 (Python) (0) | 2022.11.10 |
---|---|
[BaekJoon] 24405번 : Eye of Sauron (Python) (0) | 2022.11.09 |
[BaekJoon] 25815번 : Cat's Age (Python) (0) | 2022.11.07 |
[BaekJoon] 11536번 : 줄 세우기 (Python) (0) | 2022.11.06 |
[BaekJoon] 23235번 : The Fastest Sorting Algorithm In The World (Python) (0) | 2022.11.05 |