-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
32 lines (23 loc) · 1.24 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Your task is to create a function called sum_arrays() in Python or addArrays in Javascript,
# which takes two arrays consisting of integers, and returns the sum of those two arrays.
# The twist is that (for example) [3,2,9] does not equal 3 + 2 + 9, it would equal '3' + '2' + '9'
# converted to an integer for this kata, meaning it would equal 329.
# The output should be an array of the the sum in a similar fashion to the input
# (for example, if the sum is 341, you would return [3,4,1]).
# Examples are given below of what two arrays should return.
# [3,2,9],[1,2] --> [3,4,1]
# [4,7,3],[1,2,3] --> [5,9,6]
# [1],[5,7,6] --> [5,7,7]
# If both arrays are empty, return an empty array.
# In some cases, there will be an array containing a negative number as the first index in the array. In this case treat the whole number as a negative number. See below:
# [3,2,6,6],[-7,2,2,8] --> [-3,9,6,2] # 3266 + (-7228) = -3962
def sum_arrays(array1, array2):
if not array1 + array2:
return []
array1 = 0 if not array1 else int(''.join(list(map(str, array1))))
array2 = 0 if not array2 else int(''.join(list(map(str, array2))))
s = array1 + array2
lst = list(map(int, str(abs(s))))
if s < 0:
lst[0] = -lst[0]
return lst