9. Python, CheckiO Elementary_Replace First
In a given list the first element should become the last one. An empty list or list with only one element should stay the same.
Input: List.
Output: Iterable.
Example:
replace_first([1, 2, 3, 4]) == [2, 3, 4, 1]
replace_first([1]) == [1]
-> Solve it
주어진 목록에서 첫 번째 요소가 마지막 요소가 되어야 한다.
요소가 하나만 있는 빈 목록이나 목록은 동일하게 유지되어야 한다.
5. 구성이 다른 자료형(list형)
5.1 list형(list type) num_list = [1, 2, 3]
5.2 list형의 index(0, 1, 2, 3, 4, 5 등의 순서)
5.3 list형의 연산
5.3.1 list형 간의 연산자 : ‘+’와 ‘*’(문구 병합, 반복)
5.3.2 list형의 일부분 추출 : Slice 연산(:범위 일부분 추출) -> slice[n:m]
5.3.3 list형 원소변경(spell[3]=’a’)
def replace_first(items: list) -> Iterable:
return items[1:]+items[:1]
def replace_first(items: list) -> Iterable:
return items[1:]+items[0:1]
# 코드 리뷰 후
# pop = pop()은 리스트의 맨 마지막 요소를 돌려주고 그 요소는 삭제한다.
# .append() 리스트에 요소 추가 append(x)는 리스트의 맨 마지막에 x를 추가하는 함수이다.
if len(items) > 0:
element1 = []
element.append(items[0])
itmes.pop(0) # 리스트의 맨 마지막 요소를 삭제한다.
summ = (itmes + element1)
return summ
else:
return []
if __name__ == '__main__':
print("Example:")
print(list(replace_first([1, 2, 3, 4])))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(replace_first([1, 2, 3, 4])) == [2, 3, 4, 1]
assert list(replace_first([1])) == [1]
assert list(replace_first([])) == []
print("Coding complete? Click 'Check' to earn cool rewards!")