-
가운데 글자 가져오기프로그래밍/알고리즘 2021. 3. 23. 11:31
내가 한 풀이 제출 테스트에서 2번에서 계속 실패했다.
def solution(s): answer = '' if len(s) < 2: answer = s return answer if len(s) % 2 == 0: # 짝수 count = (len(s) // 2) - 1 answer = s[count:-count] return answer else: # 홀수 count = int(len(s) // 2) answer = s[count:-count] return answer
다른 사람 풀이 1
def solution(s): if len(s) % 2 == 1: return s[len(s)//2] else: return s[(len(s)//2-1) : (len(s)//2+1)]
내가 짜놓은 코드와 비슷하지만 더 짧고 간결하게 만들었다.
나누기를 //로 사용하면서 정수로 만들어주는 것이 포인트이다.
다른 사람 풀이 2
def solution(s): return s[(len(s)-1)//2:len(s)//2+1]
조건문도 안쓰고 푼 사람도 있다...
728x90반응형LIST'프로그래밍 > 알고리즘' 카테고리의 다른 글
서울에서 김서방 찾기 (0) 2021.03.23 같은 숫자는 싫어 (0) 2021.03.23 문자열 다루기 기본 (0) 2021.03.22 2016년 (0) 2021.03.22 프로그래머스 - k번째 수 (0) 2021.03.22