나는 이렇게 학습한다/Algorithm & SQL
0205. pick a set of first elements
daco2020
2023. 2. 5. 21:47
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]