코딩테스트

하루 코테 3개 풀기 - 10일 차

songsua 2024. 12. 19. 00:08

1. 문자열 나누기

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

def solution(s):
    answer = 0
    x = s[0]
    other_count = same_count = 0
    
    for idx, letter in enumerate(s) :
        if letter == x :
            same_count += 1
        else : 
            other_count += 1
            
        if same_count ==  other_count :
            answer += 1
            if idx != len(s) - 1 :
                x = s[idx + 1]
            other_count = same_count = 0
    if same_count !=  other_count :
             answer += 1

    return answer

 

2. 둘만의 암호

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

def solution(s, skip, index):
    answer = ''
    for let in s :
        count = index
        while count :
            if let == 'z' :
                let = 'a'
            else :
                let = chr(ord(let) + 1 )
            if let not in skip :
                count -= 1
        answer += let
    return answer

 

3. 프로세스 

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

from collections import deque

def solution(priorities, location):
    que = deque([(priorities, idx) for idx, priorities in enumerate(priorities)])
    answer = 0
    while que :
        current = que.popleft()
        if any(current[0] < item[0] for item in que) :
            que.append(current)
        else :
            answer +=1
            if current[1] == location :
                return answer
  1. 프로세
1. enumerate 를 사용해서 우선순위의 idx위치를 함께 추출 하여 큐에 저장
2. 맨 앞에 있는 값을 뽑아서 (가장 왼쪽) 인덱스 0 의 값 보다 우선순위가 작으면 append를 사용하여 맨 오른쪽으로 보냄
3. 만약 현재 값이 우선순위가 현재값보다 낮다면, 위치값인 answer 을 1더하고  만약 요청하는 값인 location  값이 현재값의 1번 인덱스에 저장된 idx 값이랑 같지 않으면 while 문에 의해 다음 left 값을 찾고 점점 옆으로 더해가면서  location  값에 일치하는 값을 찾아간다.