[프로그래머스]할인 행사(Python)

2022. 11. 12. 02:27·CodingTest/Programmers

0. 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/131127

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 연속된 10일의 물품의 종류와 개수가

2) 주어진 물품의 종류(want)와 개수(number) 같은 날의 수 구하기

 

2. 입출력

# input
want = ["banana", "apple", "rice", "pork", "pot"]
number = [3, 2, 2, 2, 1]
discount = ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]

# output
result = 3

 

3. 코드

def solution(want, number, discount):
    answer = 0

    for i in range(len(discount)-9):
        sub = discount[i:i+10]                      # 10개씩 나누기
        flag = 0                                    # 개수가 동일한지 확인하는 변수
        for j in range(len(want)):                  # 물품 종류만큼 반복
            if sub.count(want[j]) != number[j]:     # 나눈 물품 종류의 개수와 원하는 물품 종류의 개수가 다르면
                flag = 1                            # flag를 1로 처리

        if flag == 0:                               # 개수가 모두 같으면
            answer += 1                             # 정답에 1 추가

    return answer

 

실행 결과

 

4. 회고

이 문제의 핵심은 'key-value' 이다.

처음에는 주어진 물품의 종류와 개수를 통해서 새롭게 배열을 생성했는데, count 함수를 이용하여 물품의 종류별 개수를 쉽게 셀 수 있는 것이 특징이다. 연속된 10일의 물품을 새로운 배열로 생성한 다음, 물품의 종류(want)를 기준으로 새로운 배열에서 물품의 개수를 구하여 물품의 개수(number)와 비교하는 방식으로 문제를 해결하였다.

 

5. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/discount_offer.py

 

GitHub - iihye/Algorithm: Solving algorithm problems

Solving algorithm problems. Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'CodingTest > Programmers' 카테고리의 다른 글

[프로그래머스]기능 개발(Python)  (0) 2022.11.18
[프로그래머스]롤케이크 자르기(Python)  (0) 2022.11.13
[프로그래머스]베스트앨범(Python)  (0) 2022.11.09
[프로그래머스]전화번호 목록(Python)  (0) 2022.11.06
[프로그래머스]체육복(Python)  (0) 2022.10.30
'CodingTest/Programmers' 카테고리의 다른 글
  • [프로그래머스]기능 개발(Python)
  • [프로그래머스]롤케이크 자르기(Python)
  • [프로그래머스]베스트앨범(Python)
  • [프로그래머스]전화번호 목록(Python)
iihye_
iihye_
  • iihye_
    hye-log
    iihye_
    • 분류 전체보기 (293)
      • Development (17)
        • Spring (3)
        • Python (5)
        • JavaScript (4)
        • React (4)
        • Next.js (1)
      • Infra (15)
        • Docker (1)
        • Jenkins (2)
        • Nginx (3)
        • JBoss (1)
        • Windows (3)
        • Linux (3)
        • Kafka (2)
      • Database (1)
        • MongoDB (1)
      • Tools (1)
        • Git (0)
        • Github (1)
      • CodingTest (125)
        • Programmers (38)
        • Baekjoon (52)
        • SWEA (27)
        • Jungol (4)
        • Codetree (1)
        • Goorm (3)
      • Education (99)
        • SSAFY 10기 (5)
        • AI Tech 4기 (94)
      • NomadCoder (35)
        • (JS)크롬 앱 만들기 (35)
      • Notice (0)
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.5
iihye_
[프로그래머스]할인 행사(Python)
상단으로

티스토리툴바