Development Artist

[COS Pro 1급, Python] 2차 8번 : 규칙에 맞는 배열 구하기 본문

Algorithm/COS

[COS Pro 1급, Python] 2차 8번 : 규칙에 맞는 배열 구하기

JMcunst 2022. 2. 25. 10:34
728x90
반응형

문제 유형

 한줄 수정

난이도

 easy

Note 

 1. 배열 양끝에서 하나씩 번갈아가면서 원소 선택하는 알고리즘. 

 

Code

# -*- coding: utf-8 -*-
# UTF-8 encoding when using korean
# 2차 8번

def solution(arr):
	left, right = 0, len(arr) - 1
	idx = 0
	answer = [0 for _ in range(len(arr))]
	while left <= right:
		if idx % 2 == 0:
			answer[idx] = arr[left]
			left += 1
		else:
			answer[idx] = arr[right]
			right -= 1
		idx += 1
	return answer

arr = [1, 2, 3, 4, 5, 6]
ret = solution(arr)

print("solution 함수의 반환 값은", ret, "입니다.")

 

※ 가끔 코드 중 print(~)가 있습니다. 정리 못한 점 죄송합니다.

728x90
반응형
Comments