반응형
Notice
Recent Posts
Recent Comments
Link
동캄의 코딩도장
프로그래머스 level2 [영어 끝말잇기] 파이썬 본문
반응형
https://programmers.co.kr/learn/courses/30/lessons/12981
코딩테스트 연습 - 영어 끝말잇기
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
#프로그래머스 영어 끝말잇기
def solution(n, words):
lst=[]
cnt=-1
previous=words[0][0]
for i in range(len(words)):
if previous==words[i][0]:
if words[i] not in lst:
lst.append(words[i])
previous=words[i][-1]
else:
cnt=(i)
break
else:
cnt=(i)
break
if cnt==-1:
answer=[0,0]
else:
answer=[(cnt%n)+1,(cnt//n)+1]
return answer
previous에 이전 단어의 마지막 글자를 저장한다. previous와 다음 단어의 첫 글자가 일치하고, lst에 단어가 존재하면 lst에 단어를 추가하고, previous를 갱신한다.
그렇지 않다면, 반복문을 탈출하고, 순서와 차례를 계산한다.
반응형
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level2 [2개 이하로 다른 비트] 파이썬 (0) | 2021.12.08 |
---|---|
프로그래머스 level2 [예상 대진표] 파이썬 (0) | 2021.12.07 |
프로그래머스 level2 [모음사전] 파이썬 (0) | 2021.12.07 |
프로그래머스 level2 [주식가격] 파이썬 (0) | 2021.12.07 |
프로그래머스 level2 [괄호 회전하기] 파이썬 (0) | 2021.12.06 |