[프로그래머스] 1단계 (1)

a·2020년 10월 9일
0

알고리즘

목록 보기
1/3
post-custom-banner

> 모든 레코드 조회하기

select * from ANIMAL_INS order by ANIMAL_ID;

>크레인 인형뽑기 게임

def solution(board, moves):
    answer = 0
    
    board2 = [] 
    for i in range(len(board)):
        line = []
        for j in range(len(board)):
            if board[j][i]!= 0:line.append(board[j][i])
        board2.append(line)
    #[[4, 3], [2, 2, 5], [1, 5, 4, 1], [4, 3], [3, 1, 2, 1]]
    
    basket = []
    count = 0
    for i in moves:
        if len(board2[i-1])==0:continue
        else:basket.append(board2[i-1].pop(0))
        
        if len(basket)>=2 and basket[len(basket)-1] == basket[len(basket)-2]:
            basket.pop(-1)
            basket.pop(-1)
            count += 1        
    
    answer = count*2
    return answer

> 두 개 뽑아서 더하기

def solution(numbers):
    answer = []
    for i in range(len(numbers)):
        for j in range(i+1,len(numbers)):
            answer.append(numbers[i]+numbers[j])
    answer = list(set(answer))
    return sorted(answer,key = lambda x:x)

> 최댓값 구하기

select datetime from animal_ins where datetime = (select max(datetime) from animal_ins);

> 완주하지 못한 선수

def solution(participant, completion):
    answer = ''
    dic=dict() #빈 딕셔너리
    for i in range(len(participant)):
        word=participant[i]
        dic[word]=dic.get(word,0)+1 #참가하면 +1 #get(word,0) word가 비어있으면 0을 가져온다...
    for i in range(len(completion)):
        word=completion[i]
        dic[word]=dic.get(word)-1 #완주하면 다시 -1 동명이인 포함 완주하면 0된다
    for key,val in dic.items():
        if val!=0:
            answer=key
            break
    return answer

> 모의고사

def solution(answers):
    answer = []    
    a = [1,2,3,4,5]*2000
    b = [2,1,2,3,2,4,2,5]*1250
    c = [3,3,1,1,2,2,4,4,5,5]*1000
    score = [[1,0],[2,0],[3,0]]
    
    i = 0    
    for ans in answers:
        if ans==a[i]:
            score[0][1]+=1
        if ans==b[i]:
            score[1][1]+=1
        if ans==c[i]:
            score[2][1]+=1
        i+=1
        
    score2 = sorted(score, key=lambda x:(-x[1],x[0]))
    for sco in score2:
        if sco[1]==score2[0][1]:
            answer.append(sco[0])   
    answer = sorted(answer)
    return answer

> K번째 수

import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for(int i=0;i<commands.length;i++){
            int[] arr = new int[commands[i][1]-commands[i][0]+1];            
            for(int j=0;j<arr.length;j++){
                arr[j] = array[commands[i][0]+j-1];           
            }
            Arrays.sort(arr);
            answer[i] = arr[commands[i][2]-1];
        }
        return answer;
    }
}

> 체육복

def solution(n, lost, reserve):
    answer = 0
    student = [5]+[1]*n+[5] #크기가 n인 배열 +([5]는 index out of 어쩌고떔에...)
    for l in lost: #잃어버린학생 -1
        student[l]-=1
    for r in reserve: #여분있는학생 +1
        student[r]+=1
    for r in reserve:
        if student[r]==2 and student[r-1]==0: #우선 앞에꺼
            student[r]-=1
            student[r-1]+=1
        elif student[r]==2 and student[r-1]!=0 and student[r+1]==0: #그다음 뒤에꺼
            student[r]-=1
            student[r+1]=1

    answer = student.count(1)+student.count(2)
    return answer

> 2016년

def solution(a, b):
    answer = ''
    temp = b
    for i in range(1,a):
        if i in [1,3,5,7,8,10,12]:temp+=31
        if i in [2]:temp+=29
        if i in [4,6,9,11]: temp+=30
    if temp%7 == 1:return 'FRI'
    if temp%7 == 2:return 'SAT'
    if temp%7 == 3:return 'SUN'
    if temp%7 == 4:return 'MON'
    if temp%7 == 5:return 'TUE'
    if temp%7 == 6:return 'WED'
    else:return 'THU'

> 가운데 글자 가져오기

def solution(s):
    answer = ''
    if len(s)%2 == 0:
        answer = s[int(len(s)/2)-1:int(len(s)/2)+1]
    if len(s)%2 == 1:
        answer = s[int(len(s)/2)]
    return answer

같은 숫자는 싫어

def solution(arr):
    answer = []
    for i in range(len(arr)):
        if i==0:
            answer.append(arr[i])
            continue
        if arr[i]!=arr[i-1]:
            answer.append(arr[i])
    return answer

나누어 떨어지는 숫자 배열

def solution(arr, divisor):
    answer = []
    arr.sort()
    for a in arr:
        if a%divisor == 0:
            answer.append(a)
    if len(answer)==0: return [-1]
    else: return answer

두 정수 사이의 합

def solution(a, b):
    answer = 0
    numA = min(a,b)
    numB = max(a,b)
    for i in range(numA,numB+1):
        answer += i
    return answer

문자열 내 마음대로 정렬하기

def solution(strings, n):
    answer = []
    answer = sorted(strings, key=lambda x:(x[n],x))
    return answer

문자열 내 p와 y의 개수

def solution(s):
    answer = True
    s = s.upper()
    if s.count('P')==s.count('Y'): return True
    else: return False

문자열 내림차순으로 배치하기

def solution(s):
    answer = ''
    answer = "".join(sorted(s,reverse=True))
    return answer

문자열 다루기 기본

def solution(s):
    status = len(s)==4 or len(s)==6
    return status and s.isdigit()

서울에서 김서방 찾기

def solution(seoul):
    answer = ["김서방은 " , str(seoul.index('Kim')) , "에 있다"]
    answer = "".join(answer)
    return answer
profile
시작!
post-custom-banner

0개의 댓글