본문 바로가기
Engineering WIKI/Programmers

[프로그래머스] 영어 끝말잇기

by wonos 2022. 6. 1.
 

코딩테스트 연습 - 영어 끝말잇기

3 ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"] [3,3] 5 ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"] [0,0]

programmers.co.kr

내 풀이 1차시도 실패

def solution(n, words):
    answer = []
    
    word_list = list()
    last_word = list()
    order = 0
    rotation = 0
    for word in words:

        if len(last_word) != 0 and last_word[-1] != word[0]:
            return [order, rotation]
            break
            
        order += 1
        if word not in word_list:
            last_word.append(word[-1])
            word_list.append(word)
            
                        
            if order == n:
                order = 1
                rotation += 1
        
            
        else:
            return [order, rotation]
            break
            
    return [0, 0]

내 풀이 두번째 시도 (성공)

def solution(n, words):
    
    word_list = list()
    order, rotation = 0, 0
    
    word_list.append(words[0])
    last_word = words[0][-1]
    
    for i in range(1, len(words)):
        if words[i] not in word_list and words[i][0] == last_word:
            word_list.append(words[i])
            last_word = words[i][-1]

        else:
            order = (i % n) + 1
            rotation = (i // n) + 1
            break
            
    return [order, rotation]

다른 사람 풀이

def solution(n, words):
    for p in range(1, len(words)):
        if words[p][0] != words[p-1][-1] or words[p] in words[:p]: return [(p%n)+1, (p//n)+1]
    else:
        return [0,0]