I wanted to implement the following Python function to add arrays in Java. I have several doubts with the loop. Working in Eclipse when I try to make the loop for i in range(len(numbers1), len(numbers2)):
I have implemented it in the following way for (; i < number1.length && i < number2.length; i++)
. I have put the ;
because it indicated that the loop requires a structure made up of 3 "parts". In the same way, to implement the statement, elif
it asked me to close the previous structure, the if
and the for
with the proper}
Python code:
def sum_lists(numbers1, numbers2):
result = [0 for i in range(max(len(numbers1), len(numbers2)))]
for i in range(min(len(numbers1), len(numbers2))):
result[i] = numbers1[i] + numbers2[i]
if len(numbers1) > len(numbers2):
for i in range(len(numbers2), len(numbers1)):
result[i] = numbers1[i]
elif len(numbers1) < len(numbers2):
for i in range(len(numbers1), len(numbers2)):
result[i] = numbers2[i]
return result
Java code:
public static int[] sumLists(int[] number1, int[] number2) {
int[] result = new int [Math.max(number1.length, number2.length)];
for (int i = 0; i < Math.min(number1.length, number2.length); i++) {
result[i] = number1[i] + number2[i];
if (number1.length > number2.length) {
for (; i < number2.length && i < number1.length; i++) {
result[i] = number1[i]; // Duda
}}else if (number1.length < number2.length) {
for (; i < number1.length && i < number2.length; i++) { // Duda
result[i] = number2[i];
}
}
}
return result;
}
The conditionals must not be inside the
for
, they must be executed afterwards to fill the array with the remaining elements if one of them is greater, exactly as it happens in the Python implementation.If you put them inside they will be executed on each iteration of the for. Also, the range of indices in the conditionals must be:
If numbers1 > numbers2 -> from
numbers2.length
tonumbers1.length - 1
Equivalent to
range(len(numbers2), len(numbers1))
in Python.If numbers1 < numbers2 -> from
numbers1.length
tonumbers2.length - 1
Equivalent to
range(len(numbers1), len(numbers2))
in Python.You can remove almost all braces if you want in this case, since there's only one statement under each
for
conditional o, but generally style conventions don't recommend it, it's easier to make mistakes (the indentation here is purely visual, not as in Python) and makes it difficult to maintain:By the way, a much simpler and more efficient way to do this in Python, also with an indeterminate number of lists, is to use
itertools.zip_longest
:The first problem is that you shouldn't put everything in the first for. You have another problem in the stop condition. The for should stop when the length of the largest array is reached, in your case this does not happen. Here is the correction:
Try a different logic:
The companions show other different logic, with the continuations of the result depending on which other array is greater than the first for. In any case, the for loops cannot have the format that you propose, and therefore you have to find another logic to execute it differently, such as the ones proposed by the peers or the one exposed here.
You can make it shorter and less
if
s simply by assigning smaller and larger arrays to new references accordingly, and if they are the same size it doesn't affect the behavior. Also by using clearer names you can understand it better: