728x90
[1] Problem: https://py.checkio.org/en/mission/absolute-sorting/
[2] Solution
def checkio(values: list) -> list:
# your code here
abs_sorted = sorted([abs(n) for n in values])
answers = []
for n in range(len(abs_sorted)):
if not abs_sorted[n] in values: answers.append(- abs_sorted[n])
else: answers.append(abs_sorted[n])
return answers
if __name__ == '__main__':
print("Example:")
print(checkio([-20, -5, 10, 15]))
# These "asserts" are used for self-checking and not for an auto-testing
assert checkio([-20, -5, 10, 15]) == [-5, 10, 15, -20]
assert checkio([1, 2, 3, 0]) == [0, 1, 2, 3]
assert checkio([-1, -2, -3, 0]) == [0, -1, -2, -3]
print("Coding complete? Click 'Check' to earn cool rewards!")
[3] Best Solutions
def checkio(numbers_array):
"""
The magic of the key :)
"""
return tuple(sorted(numbers_array, key=abs))
1) numbers_array를 절대값을 기준(key = abs)으로 정렬함
2) 하지만 왜 tuple을 적용하는지는 잘 모르겠음
728x90
'프로그래밍 문제 > [Python] CheckIO' 카테고리의 다른 글
[Check-IO] Time converter (0) | 2021.12.10 |
---|---|
[CheckIO] Goes Right After (0) | 2021.12.03 |
[Check-IO] Frequency Sorting (0) | 2021.04.18 |
[CheckIO] Median (0) | 2021.03.23 |
[CheckIO] Remove All After (0) | 2021.03.22 |
댓글