Last active
August 23, 2021 03:35
-
-
Save cjnghn/dc78848fb6d7657aa0336ded63b4f73b to your computer and use it in GitHub Desktop.
프로그래머스 풀이 (Level 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 모든 레코드 조회하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59034 | |
SELECT * FROM ANIMAL_INS ORDER BY ANIMAL_ID; | |
## 역순 정렬하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59035 | |
SELECT NAME, DATETIME FROM ANIMAL_INS ORDER BY ANIMAL_ID DESC; | |
## 아픈 동물 찾기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59036 | |
SELECT ANIMAL_ID, NAME | |
FROM ANIMAL_INS | |
WHERE INTAKE_CONDITION = 'Sick' | |
ORDER BY ANIMAL_ID; | |
## 어린 동물 찾기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59037 | |
SELECT ANIMAL_ID, NAME | |
FROM ANIMAL_INS | |
WHERE INTAKE_CONDITION != 'Aged' | |
ORDER BY ANIMAL_ID; | |
## 동물의 아이디와 이름 | |
# https://programmers.co.kr/learn/courses/30/lessons/59403 | |
SELECT ANIMAL_ID, NAME | |
FROM ANIMAL_INS | |
ORDER BY ANIMAL_ID; | |
## 여러 기준으로 정렬하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59404 | |
SELECT ANIMAL_ID, NAME, DATETIME | |
FROM ANIMAL_INS | |
ORDER BY NAME, DATETIME desc | |
## 상위 n개 레코드 | |
# https://programmers.co.kr/learn/courses/30/lessons/59405 | |
SELECT NAME | |
FROM ANIMAL_INS | |
ORDER BY DATETIME | |
LIMIT 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 최댓값 구하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59415 | |
SELECT DATETIME | |
FROM ANIMAL_INS | |
ORDER BY DATETIME desc | |
LIMIT 1; | |
## 최솟값 구하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59038 | |
SELECT DATETIME | |
FROM ANIMAL_INS | |
ORDER BY DATETIME | |
LIMIT 1; | |
## 동물 수 구하기 | |
# https://programmers.co.kr/learn/courses/30/lessons/59406 | |
SELECT COUNT(*) | |
FROM ANIMAL_INS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 내적 | |
# https://programmers.co.kr/learn/courses/30/lessons/70128 | |
def solution(a, b): | |
prod = 0 | |
for n, m in zip(a, b): | |
prod += n*m | |
return prod | |
## 다른 사람풀이 | |
def solution(a, b): | |
return sum([x*y for x, y in zip(a, b)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(lottos, win_nums): | |
rank = [6,6,5,4,3,2,1] | |
count_zero = lottos.count(0) | |
common_count = 0 | |
for num in lottos: | |
if num in win_nums: | |
common_count += 1 | |
return [rank[common_count + count_zero], rank[common_count]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://programmers.co.kr/learn/courses/30/lessons/42840 | |
def solution(answers): | |
scores = [0]*4 | |
w1 = [1,2,3,4,5] | |
w2 = [2,1,2,3,2,4,2,5] | |
w3 = [3,3,1,1,2,2,4,4,5,5] | |
for idx, ans in enumerate(answers): | |
r1, r2, r3 = w1[idx%5], w2[idx%8], w3[idx%10] | |
if r1 == ans: scores[1] += 1 | |
if r2 == ans: scores[2] += 1 | |
if r3 == ans: scores[3] += 1 | |
return [idx for idx, val in enumerate(scores) if val == max(scores) and idx != 0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 문자열 압축 (다시 풀어야함) | |
# https://programmers.co.kr/learn/courses/30/lessons/60057 | |
def solution(s): | |
length = len(s) | |
cand = [length] # 1-len 까지 압축했을 때 길이 값 | |
for size in range(1, length): | |
compressed = '' | |
splited = [s[i:i+size] for i in range(0, length, size)] | |
count = 1 | |
for j in range(1, len(splited)): | |
prev, curr = splited[j-1], splited[j] | |
if prev == curr: | |
count += 1 | |
else: | |
compressed += (str(count) + prev) if count > 1 else prev | |
count = 1 | |
compressed += (str(count) + splited[-1]) if count > 1 else splited[-1] | |
cand.append(len(compressed)) | |
return min(cand) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 위클리 챌린지 1주차 | |
# https://programmers.co.kr/learn/courses/30/lessons/82612 | |
def solution(price, money, count): | |
fee = 0 | |
for i in range(count): | |
fee += price*(i + 1) | |
return fee - money if fee - money > 0 else 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 숫자 문자열과 영단어 | |
# https://programmers.co.kr/learn/courses/30/lessons/81301 | |
def solution(s): | |
words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] | |
word2num = {num: str(idx) for idx, num in enumerate(words)} | |
res = [] | |
i = 0 | |
while i < len(s): | |
if s[i].isdigit(): | |
res.append(s[i]) | |
i += 1 | |
else: | |
en = i + 1 | |
while en < len(s) and not s[en].isdigit(): | |
if s[i:en] in word2num: break | |
en += 1 | |
res.append(word2num[s[i:en]]) | |
i = en | |
return int(''.join(res)) | |
## 다른 사람 풀이 | |
num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"} | |
def solution(s): | |
answer = s | |
for key, value in num_dic.items(): | |
answer = answer.replace(key, value) | |
return int(answer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def solution(new_id): | |
new_id = re.sub('[^a-z0-9\-_.]', '', new_id.lower()) | |
tmp = [] | |
for i in range(len(new_id)): | |
if len(tmp) and new_id[i] == new_id[i - 1] and new_id[i] == '.': | |
continue | |
tmp.append(new_id[i]) | |
# remove head, tail '.' | |
while len(tmp): | |
if tmp[0] == '.': tmp.pop(0) | |
elif tmp[-1] == '.': tmp.pop() | |
else: break | |
if len(tmp) == 0: tmp.append('a') | |
if len(tmp) > 15: | |
tmp = tmp[:15] | |
if tmp[-1] == '.': tmp.pop() | |
while len(tmp) < 3: | |
tmp.append(tmp[-1]) | |
return ''.join(tmp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 완주하지 못한 선수 | |
# https://programmers.co.kr/learn/courses/30/lessons/42576 | |
# 아래처럼 짜면 시간 초과 | |
def solution(participant, completion): | |
res = participant.copy() | |
for completed in completion: | |
res.remove(completed) | |
return res | |
# 그렇다면 해쉬를 이용해야 할 것 같다는 생각이 듬. | |
import collections | |
def solution(participant, completion): | |
counter = collections.Counter(participant) | |
for completed in completion: | |
counter[completed] -= 1 | |
for k, v in counter.items(): | |
if v == 1: return k | |
## 다른 사람의 풀이 | |
import collections | |
def solution(participant, completion): | |
answer = collections.Counter(participant) - collections.Counter(completion) | |
return list(answer.keys())[0] | |
## 또는 정렬의 성질을 이용해도 O(nlogn)이라는 걸.. | |
def solution(participant, completion): | |
participant.sort(); completion.sort() | |
for p, c in zip(participant, completion): | |
if p != c: | |
return p | |
return participant[-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 위클리 챌린지 4주차 | |
# https://programmers.co.kr/learn/courses/30/lessons/84325?language=python3 | |
from typing import List | |
def solution(table: List[str], languages: List[str], preference: List[int]): | |
recommended = [] | |
table = [[s.split()[0], s.split()[1:]] for s in table] | |
for field, langs in table: | |
score = 0 | |
for idx, lang in enumerate(langs): | |
# 언어 선호도 X 직업군 언어 점수 | |
if lang in languages: | |
score += (preference[languages.index(lang)])*(5 - idx) | |
recommended.append([score, field]) | |
recommended.sort(key=lambda x: (-x[0], x[1])) | |
return recommended[0][1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment