2023. 8. 15. 14:59ㆍHackerRank-Python
collections.Counter() | HackerRank
Use a counter to sum the amount of money earned by the shoe shop owner.
www.hackerrank.com
문제
Task
Mr.R is a shoe shop owner. His shop has S number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are N number of customers who are willing to pay x(i) amount of money only if they get the shoe of their desired size.
Your task is to compute how much money Mr.R earned.
Input Format
The first line contains X, the number of shoes.
The second line contains the space separated list of all the shoe sizes in the shop.
The third line contains N, the number of customers.
The next N lines contain the space separated values of the shoe size desired by the customer and x(i), the price of the shoe.
Output Format
Print the amount of money earned by Mr.R.
=> Mr.R이 가진 신발 수, 신발 사이즈들, 고객들의 희망 사이즈와 지불용의 가격들을 줄거니까 Mr.R의 매출액을 구하라.
코드
from collections import Counter
X = int(input())
x = Counter(list(map(int, input().split())))
N = int(input())
money = 0
for _ in range(N):
a, b = map(int, input().split())
if x[a] != 0:
money += b
x[a] = x[a]-1
print(money)
노트
collections.Counter()
from collections import Counter
myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
print(Counter(myList))
# Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
print(Counter(myList).items()) # items()로 key,value 쌍 튜플로 확인
# [(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
print(Counter(myList).keys()) # keys()로 key값들 확인
# [1, 2, 3, 4, 5]
print(Counter(myList).values())# values()로 value값들 확인
# [3, 4, 4, 2, 1]
- 요소가 key, 각 요소의 count()가 value인 dictionary 만듦
dict에 관하여
- 개별 value 수정
dict = {one:1, three:3}
dict[one] = 3
print(dict)
# {one:3, three: 3}
- key, value 쌍 얻기
dict = {one:1, three:3}
print(dict.items())
# (one, 3), (three: 3)
- key로 value찾기
dict = {one:1, three:3}
print(dict[one]) # one이 dict의 key값에 없으면 에러
# 1
print(dict.get(one)) # one이 dict의 key값에 없으면 None 리턴
# 1
- key, value 삭제
dict_1 = {'one':1, 'three':3}
del(dict_1['one']) # key값이 'one'인 쌍 삭제
print(dict_1)
# {'three': 3}
dict_1.clear() # 그냥 사전 비우기. 다 사라짐
print(dict_1)
# {}
두 번째 시도
from collections import Counter
X = int(input())
shoes = Counter(map(int, input().split()))
N = int(input())
sales = 0
for i in range(N):
size, price = map(int, input().split())
if shoes[size]:
sales += price
shoes[size] -= 1
print(sales)
- if 조건을 직접적인 불린값으로 쓰지 않음!
참조
02-5 딕셔너리 자료형
사람은 누구든지 이름 = 홍길동, 생일 = 몇 월 며칠 등과 같은 방식으로 그 사람이 가진 정보를 나타낼 수 있다. 파이썬은 영리하게도 이러한 대응 관계를 나타낼 …
wikidocs.net
[Python] Dictionary 값 수정, 추가, 삭제
Dictionary 값 수정리스트와 마찮가지로 해당 key의 value를 변경해주면 된다.ex) dict = { 'one' : 0, 'two' : 2 }dict['one'] = 1 Dictionary 추가리스트와는 달리 Dictionary변수에 key와 value를 추가하면 된다.ex)dict = { '
devinside.tistory.com
HackerRank collections.Counter() solution in Python
hackerrank collections.counter() solution in python 2, python3 and pypy pypy3 programming language with practical program code example explaination
programs.programmingoneonone.com
'HackerRank-Python' 카테고리의 다른 글
Prepare > Python > Collections > Collections.namedtuple() (0) | 2023.08.17 |
---|---|
Prepare > Python > Collections > DefaultDict Tutorial (0) | 2023.08.16 |
Prepare > Python > Itertools > itertools.combinations_with_replacement() (0) | 2023.08.14 |
Prepare > Python > Itertools > itertools.combinations() (0) | 2023.08.13 |
Prepare> Python > Itertools > itertools.permutations() (0) | 2023.08.05 |