Algorithm Practice: Sum Two Arrays
Steven Frasica
Posted on October 29, 2020
I collaborated on this problem with a group of friends and it was helpful to get so many different perspectives on the problem as we discussed and approached it. As a result, the solution felt a bit scattered and while it's not the most optimal, the main takeaway was the successful collaboration of four people.
The Problem
Found on Codewars, the problem reads as follows:
"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."
Ex.
[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
Approach
There are a couple of cases initially considered, such as if both arrays given are empty, then we want to return an empty array. If there is only one empty array, then we simply want to return the other array.
Once we consider those cases, when we are actually working with an array containing elements, it seemed simple to change the elements into an integer. First, we join the elements into a string, then use parseInt()
to turn the string into an integer. We then want to add those two sets of integers we now have together.
Then we take that sum, and convert it to a single array.
We also declare an empty array that we'll work with a little later.
We'll loop through the single array made up of the sum, and explain that more with the actual coding part.
Since we want our answer to be an array of elements, we'll push the elements from the sum array into our empty array declared earlier, and return that array with the new elements inside.
Solution
Here we account for the cases of having two or one empty arrays.
function addArrays(array1, array2) {
if(array1.length < 1 && array2.length < 1){
return []
}
else if( array1.length < 1 ){
return array2
}
else if( array2.length < 1){
return array1
}
Here we convert each array into an integer and add them together, setting it to the variable sum
.
let num1 = parseInt(array1.join(''));
let num2 = parseInt(array2.join(''));
let sum = num1 + num2;
We convert our sum into a single array. We declare an empty array we will use for our final answer.
let strArr = sum.toString().split('')
let newArr = []
We have to check for negative numbers, so we used this conditional to look at the first element in our array. If the element is a negative sign, then we are essentially tacking on that negative sign to the first integer in the array, and then removing the negative sign element.
if(strArr[0] === '-'){
strArr[1] = '-'+ strArr[1]
strArr.shift()
}
Lastly, we use a for...of loop to iterate through our array and we convert each character of the array into an integer using parseInt()
and push the integer into our newArr, which was previously empty.
Make sure to return the newArr which holds the separate integers as elements as the problem requested.
for(let char of strArr){
newArr.push(parseInt(char))
}
return newArr
}
Resources
-Collaboration with others!
Posted on October 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024