반응형
Write a function to get the first element(s) of a sequence. Passing a parameter n (default=1) will return the first n element(s) of the sequence.
If n == 0 return an empty sequence []
Examples
arr = ['a', 'b', 'c', 'd', 'e']
first(arr) # --> ['a']
first(arr, 2) # --> ['a', 'b']
first(arr, 3) # --> ['a', 'b', 'c']
first(arr, 0) # --> []
Solution:
def first(seq: list[str], n:int = 1) -> list[str]:
if n < 1:
return []
return seq[:n]
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0207. Divide and Conquer (0) | 2023.02.08 |
---|---|
0206. Find the nth Digit of a Number (0) | 2023.02.07 |
0204. Filter the number (0) | 2023.02.04 |
0203. Parse float (0) | 2023.02.03 |
0202. Beginner Series #1 School Paperwork (0) | 2023.02.02 |