KATA#62

codataffee·2024년 6월 14일
0

CODEKATA

목록 보기
62/114
post-thumbnail

WHAT IS KATA?

KATA는 기술과 기술 향상에 초점을 맞춘 코드 챌린지입니다.
일부는 프로그래밍 기본 사항을 교육하는 반면 다른 일부는 복잡한 문제 해결에 중점을 둡니다.

이 용어는 The Pragmatic Programmer 라는 책의 공동 저자인 Dave Thomas 가
무술에서 일본의 카타 개념을 인정하면서 처음 만들어졌습니다.
Dave의 개념 버전은 코드 카타를 프로그래머가
연습과 반복을 통해 기술을 연마하는 데 도움이 되는 프로그래밍 연습으로 정의합니다.


- SQL


✔️ 문제 #1: The PADS

✔️ 제출 쿼리

✔️ 쿼리 분석

# 이름과 직업 맨 앞글자를 가져와 CONCAT
SELECT CONCAT(Name,'(',LEFT(Occupation, 1),')') NMOP
FROM OCCUPATIONS
ORDER BY name;

# 문자열 사이에 COUNT 함수와 LOWER 함수로 값 가져와 CONCAT
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY COUNT(Occupation);


- PYTHON


✔️ 문제 #1: 할인 행사

✔️ 제출 코드

✔️ 코드 분석

def solution(want, number, discount):
	# 원하는 제품과 갯수를 키:값으로 하는 딕셔너리 생성
    want_dict = {want[i] : number[i] for i in range(len(want))}
    # 회원 자격 10일
    member_period = 10
    answer = 0
    
    # 회원 자격(10일)동안 가능한 모든 할인 제품 비교
    for start in range(len(discount) - member_period + 1):
        count = {product : 0 for product in want}
        # 현재 확인하는 10일 기간의 할인 품목 세기
        for i in range(start, start + member_period):
            if discount[i] in count:
                count[discount[i]] += 1
        # 현재 10일 기간 동안 모든 제품의 수량이 want_dict 제품 수량보다 많은지 확인
        if all(count[product] >= want_dict[product] for product in want_dict):
        	# 조건에 부합하면 날짜(answer) 1 증가
            answer += 1

    return answer


+) 슬라이딩 윈도우 활용한 문제 해결 방법

def solution(want, number, discount):
	# want와 number 배열을 이용해 want_dict 딕셔너리 생성
    want_dict = {want[i]: number[i] for i in range(len(want))}
    want_length = len(discount)
    period_length = 10
    valid_days = 0
    
    # 원하는 모든 제품의 개수를 0으로 초기화한 current_count 딕셔너리
    current_count = {product: 0 for product in want}
    # 초기 윈도우 설정
    # 처음 10일 동안의 discount 배열을 탐색하여 current_count를 업데이트
    for i in range(period_length):
        if discount[i] in current_count:
            current_count[discount[i]] += 1
    
    # 첫 번째 윈도우 검사
    # 현재 윈도우에서 모든 제품의 수량이 원하는 수량을 만족하는지 확인
    if all(current_count[product] >= want_dict[product] for product in want_dict):
        valid_days += 1

    # 슬라이딩 윈도우 적용
    # 윈도우를 한 칸씩 이동하며 이전 윈도우의 첫 번째 제품을 제거하고, 
    # 새로운 윈도우의 마지막 제품을 추가
    for start_day in range(1, want_length - period_length + 1):
        # 윈도우의 시작 부분 제거
        if discount[start_day - 1] in current_count:
            current_count[discount[start_day - 1]] -= 1
        
        # 윈도우의 끝 부분 추가
        if discount[start_day + period_length - 1] in current_count:
            current_count[discount[start_day + period_length - 1]] += 1
        
        # 현재 윈도우 검사
        # 각 윈도우마다 원하는 제품의 수량을 만족하는지 확인
        if all(current_count[product] >= want_dict[product] for product in want_dict):
            valid_days += 1
    
    return valid_days

✔️ CHECK POINT

  • PYTHON

    • 슬라이딩 윈도우

      def solution(want, number, discount):
      	# want와 number 배열을 이용해 want_dict 딕셔너리 생성
       want_dict = {want[i]: number[i] for i in range(len(want))}
       want_length = len(discount)
       period_length = 10
       valid_days = 0
       
       # 원하는 모든 제품의 개수를 0으로 초기화한 current_count 딕셔너리
       current_count = {product: 0 for product in want}
       # 초기 윈도우 설정
       # 처음 10일 동안의 discount 배열을 탐색하여 current_count를 업데이트
       for i in range(period_length):
           if discount[i] in current_count:
               current_count[discount[i]] += 1
       
       # 첫 번째 윈도우 검사
       # 현재 윈도우에서 모든 제품의 수량이 원하는 수량을 만족하는지 확인
       if all(current_count[product] >= want_dict[product] for product in want_dict):
           valid_days += 1
      
       # 슬라이딩 윈도우 적용
       # 윈도우를 한 칸씩 이동하며 이전 윈도우의 첫 번째 제품을 제거하고, 
       # 새로운 윈도우의 마지막 제품을 추가
       for start_day in range(1, want_length - period_length + 1):
           # 윈도우의 시작 부분 제거
           if discount[start_day - 1] in current_count:
               current_count[discount[start_day - 1]] -= 1
           
           # 윈도우의 끝 부분 추가
           if discount[start_day + period_length - 1] in current_count:
               current_count[discount[start_day + period_length - 1]] += 1
           
           # 현재 윈도우 검사
           # 각 윈도우마다 원하는 제품의 수량을 만족하는지 확인
           if all(current_count[product] >= want_dict[product] for product in want_dict):
               valid_days += 1
       
       return valid_days

profile
커피 좋아하는 데이터 꿈나무

0개의 댓글

관련 채용 정보