코드로 우주평화

Invert values 본문

나는 이렇게 학습한다/Algorithm & SQL

Invert values

daco2020 2022. 3. 27. 22:48
반응형

Description:

Given a set of numbers, return the additive inverse of each. Each positive becomes negatives, and the negatives become positives.

invert([1,2,3,4,5]) == [-1,-2,-3,-4,-5]
invert([1,-2,3,-4,5]) == [-1,2,-3,4,-5]
invert([]) == []

You can assume that all values are integers. Do not mutate the input array/list.

 

Solution:

1. Get the numbers in the array.
2. Change positive numbers to negative numbers.
3. Change negative numbers to positive numbers.
4. Return the changed array.

 

 

def invert(lst):
    return [-i for i in lst]

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Sum of the first nth term of Series  (0) 2022.03.29
Reverse words  (0) 2022.03.28
How many pages in a book?  (0) 2022.03.26
Is n divisible by x and y?  (0) 2022.03.25
Find the divisors!  (0) 2022.03.24