[파이썬][백준 4358][💛골드 5] 생태학 (map, 소수 출력)
Categories: BOJ
Tags: Coding Test Python Algorithm
🧞♂️ 난이도
💛 골드 5
🧞♂️ 문제

🧞♂️ 내 풀이 ⭕
- 
    입력을 받으면서 dictionary로 숫자를 세어준다 
- 
    dic에 있는 key, value 를 heapq 에 넣는다 (그럼 뺄때 사전순으로 출력 된다) 
- 
    heapq에 값들을 하나씩 빼서, 전체 나무 개수로 나눠주고 출력해준다 
🧞♂️ 파이썬 코드
import sys, heapq, collections
input = sys.stdin.readline
h = []
dic = collections.defaultdict(int)
N = 0
while True:
    tree = input().rstrip()
    if not tree:
        break
    dic[tree] += 1
    N += 1
for tree, cnt in dic.items():
    heapq.heappush(h, (tree, cnt))
while h:
    tree, cnt = heapq.heappop(h)
    print('%s %.4f' %(tree, cnt/N*100))
아니면 heapq 없이 그냥 내림차순으로 정렬해서 하나씩 pop해도 될것같다 이런식으로
import sys, collections
input = sys.stdin.readline
trees = []
dic = collections.defaultdict(int)
N = 0
while True:
    tree = input().rstrip()
    if not tree:
        break
    dic[tree] += 1
    N += 1
for tree, cnt in dic.items():
    trees.append((tree, cnt))
trees.sort(reverse=True)
while trees:
    tree, cnt = trees.pop()
    print('%s %.4f' %(tree, cnt/N*100))
