문제
코딩테스트 연습 - 영어 끝말잇기
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
알고리즘
- 반복문을 통해서 단어를 확인한다.
- 규칙에 어긋나면 반복을 멈춘다.
- 확인한 단어와 입력받은 단어가 같으면 틀린 사람이 없는 것이다.
- 틀린 사람의 번호와 차례는 게임을 한 사람 수의 나머지와 나누기를 통해 구한다.
코드
import math
def solution(n, words):
answer = []
temp = []
# 반복문을 통해 단어 확인
for i in words:
# 규칙에 어긋나면 멈춘다.
if (len(temp) > 0 and temp[-1][-1] != i[0]) or i in temp:
break
temp.append(i)
if temp == words:
answer = [0,0]
else:
c = (len(temp) % n) + 1 # 번호
d = math.ceil((len(temp) + 1) / n) # 차례
answer = [c, d]
return answer
github
GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법
내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Programers' 카테고리의 다른 글
[programers] 프로그래머스(파이썬) : 스킬트리 (0) | 2022.04.26 |
---|---|
[programers] 프로그래머스(파이썬) : 점프와 순간 이동 (0) | 2022.04.25 |
[programers] 프로그래머스(파이썬) : 배달 (0) | 2022.04.24 |
[programers] 프로그래머스(파이썬) : 신규 아이디 추천 (0) | 2022.04.24 |
[programers] 프로그래머스(파이썬) : 로또의 최고 순위와 최저 순위 (0) | 2022.04.24 |