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

+1 Array

daco2020 2022. 3. 15. 01:18
반응형

Description:

Given an array of integers of any length, return an array that has 1 added to the value represented by the array.

  • the array can't be empty
  • only non-negative, single digit integers are allowed

Return nil (or your language's equivalent) for invalid inputs.

Examples

For example the array [2, 3, 9] equals 239, adding one would return the array [2, 4, 0].

[4, 3, 2, 5] would return [4, 3, 2, 6]

 

Solution:

1. If there are 10 digits or a negative number in the 'arr' array, 'None' is returned.
2. Return 'None' even if the array is empty.
3. Combine the letters in the array, convert them to numbers, and add 1.
4. Converts the combined numbers to letters and back to an array of numbers.
5. Returns an array.

 

 

def up_array(arr):
    return None if [i for i in arr if i >= 10 or i < 0] or not arr \
                else list(map(int, str(int(''.join(map(str, arr)))+1)))

 

 

 

반응형

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

English beggars  (0) 2022.03.16
Kebabize  (0) 2022.03.16
A square of squares  (0) 2022.03.13
Count the divisors of a number  (0) 2022.03.12
Maximum Length Difference  (0) 2022.03.11