[SQL 프로그래머스] 입양 시각 구하기(1) [문제][해결]SELECT hour(datetime) HOUR, count(hour(datetime)) COUNTfrom animal_outswhere hour(datetime) >= 9 and hour(datetime) [정리 및 새롭게 알게 된 점]HOUR함수를 이용해 datetime으로 부터 시간값을 추출하고,where 절에서 9시부터 19시까지의 시간에 대해 datetime의 개수(count)를 구하였다. Coding Test 2024.06.14
[Python 프로그래머스] 음양 더하기 [문제][해결]def solution(absolutes, signs): answer = 0 for absolute, sign in zip(absolutes,signs): if sign == False: absolute *= -1 answer += absolute return answer[정리 및 새롭게 알게 된 점]1. zip 함수를 써 absolutes 와 sign 배열을 함께 for loop 시킨 뒤2. sign의 원소가 False 인 경우, absolute에 -1을 곱해주고3. answer에 absolute값을 더해주었다. [다른 풀이]def solution(absolutes, signs): return sum(absolute if .. Coding Test 2024.06.14
[SQL 프로그래머스] 고양이와 개는 몇 마리 있을까 [문제][해결]SELECT animal_type, count(animal_type)from animal_insgroup by 1order by 1[정리 및 새롭게 알게 된 점]count함수로 animal_type의 개수를 구한다음, animal type으로 group by하여 인스턴스를 구분하였다. Coding Test 2024.06.13
[Python 프로그래머스] 나누어 떨어지는 숫자 배열 [문제][해결]def solution(arr, divisor): answer = [] for i in arr: if i % divisor == 0: answer.append(i) if len(answer) == 0: answer.append(-1) answer.sort() return answer[정리 및 새롭게 알게 된 점]1. 주어진 arr 배열을 for 문을 이용하여 loop를 돌리고 arr 원소가 divisor로 나누어 지는 경우 answer 리스트에 원소를 추가하였다. 2. 만약 arr 원소 i 가 divisor로 나누어 지는 경우가 없다면 answer 리스트에 -1을 넣어주었다. [다른 풀이]def solution(arr,.. Coding Test 2024.06.13
[SQL 프로그래머스] 카테고리 별 상품 개수 구하기 [문제][해결]SELECT substr(product_code,1,2) CATEGORY, count(substr(product_code,1,2)) productsfrom productgroup by 1order by 1[정리 및 새롭게 알게 된 점]1. substr함수를 이용해 product code를 추출하고 2. count 함수를 이용해 product의 개수를 구한 뒤3. group by와 order by문을 적용해 값들을 정렬시켰다. Coding Test 2024.06.12
[Python 프로그래머스] 서울에서 김서방 찾기 [문제][해결]def solution(seoul): index = seoul.index('Kim') answer = f'김서방은 {index}에 있다' return answer[정리 및 새롭게 알게 된 점]1. index 함수를 써 seoul 리스트에 있는 'Kim' 요소의 index를 구하고,2. f-string을 이용하여 string을 반환하였다. Coding Test 2024.06.12
[SQL 프로그래머스] 중성화 여부 파악하기 [문제][해결]-- IF문 이용SELECT animal_id, name, if(sex_upon_intake like '%Neutered%' or sex_upon_intake like 'Spayed%', 'O', 'X') '중성화'from animal_insorder by animal_id-- CASE WHEN문 이용SELECT animal_id, name, case when sex_upon_intake like '%Neutered%' or sex_upon_intake like 'Spayed%' then 'O' else 'X' end '중성화'from animal_insorder by animal_id[정리 및 새롭게 알게 된 점]1. If문을 이용해 중성화여부를 파악하는 방법과2. Case when 문을.. Coding Test 2024.06.11
[Python 프로그래머스] 콜라츠 추측 [문제][해결]def solution(num): if num == 1: return 0 for i in range(500): num = num / 2 if num % 2 == 0 else num*3 + 1 if num == 1: return i+1 return -1[정리 및 새롭게 알게 된 점]1. if num == 1: return 0을 통해 주어진 수가 1인 경우 0을 리턴해주었고,2. for i in range(500)을 이용해 작업을 500번 반복하는 반복문을 작성한 뒤,3, num 이 2로 나누어지면 num/2, num이 2로 나누어지지 않으면 num*3 +1을 해주었다4. 계산된 num 값이 1을 가질 때 i+1을 반환하.. Coding Test 2024.06.11
[Python 프로그래머스] 두 정수 사이의 합 [문제][해결]def solution(a, b): if a > b : a , b = b , a return sum(range(a,b+1))[정리 및 새롭게 알게 된 점]1. if문을 통해 a 가 b 보다 큰 경우 a 에 b값을 저장하고 b에 a값을 저장하여 작은 수가 a 큰 수가 b에 저장되게 하였다.2. range함수를 사용하여 a에서 b까지의 정수 리스트를 출력한다음, sum함수를 통해 출력된 값들을 더하였다. Coding Test 2024.06.10
[SQL 프로그래머스] 조건에 맞는 회원수 구하기 [문제][해결]SELECT count(*)from user_infowhere year(joined) = 2021 and (age between 20 and 29)[정리 및 새롭게 알게 된 점]1. Year 함수를 통해 2021년도 데이터를 추출하고,2. between 구문을 통해 20세 이상 29세 이하인 회원을 추출하였다. Coding Test 2024.06.10