+ 3
Array problems with addition
There are two arrays, but I want to add each index of array. The problem is different lengths. I.e. Input : A = [3, 7, 9], B = [1, 7] Output : [4, 14, 9] Please explain how to solve this problem. Any languages. https://sololearn.com/compiler-playground/cq6T2DXBu49a/?ref=app
12 Answers
+ 7
āļāļĪāļŠāļĢāļāđ āļāļĢāļīāļĒāļŠāļāļļāļĨāļ§āļāļĻāđ
The error in your code:- that you assumed both arrays `A` and `B` have the same length when accessing the elements using `A[q]` and `B[q]`.
To fix this:-add a condition to check if the index `q` is within the range of `B` before accessing its element.
See this Modified version..
https://code.sololearn.com/ccOOj7aGAwaz/?ref=app
Hope it's helpful..
+ 6
from itertools import zip_longest
C = [sum(x) for x in zip_longest(A,B, fillvalue=0)]
print(C)
+ 5
āļāļĪāļŠāļĢāļāđ āļāļĢāļīāļĒāļŠāļāļļāļĨāļ§āļāļĻāđ
So which language you prefer?
there are many languages and many term and condition to solve questions.
As Sakshi sis said..
First Try by yourself,after this if you face any problems in during learning or making code then ask here.
otherhand,
If you're having trouble in code, share your code and describe what error message you're receiving. without watching no one can figure out what's going wrong.
+ 4
This is sum of two arrays, firstly try to yourself then we can help you where are you stuck.
Hint:
A [3,7,9]
B [1,7]
Output:[4,14,9]
Now, how you can the sum
A [0][0] + B [0][0] // 3+1=4
A [0][1] + B [0][1] // 7+7 = 14
A [0][2] + B [0][2] // 9+0=9
hope you understand it.
+ 4
Here is your code wait for sometime maybe, Riya sis solved it.
https://code.sololearn.com/cIB5bJ1DUxfx/?ref=app
+ 4
Thank you very much.
+ 3
āļāļĪāļŠāļĢāļāđ āļāļĢāļīāļĒāļŠāļāļļāļĨāļ§āļāļĻāđ
Sorry, but we can't see your code see this, how you can share your code:
https://code.sololearn.com/Wek0V1MyIR2r/?ref=app
+ 2
I find my code cannot run if they run out of indexes. Should I increase index or something else?
+ 1
Can you see it?
+ 1
SK sahil Ali
Answer is already given..
So don't do any unnecessary,offtopic comments..
0
https://code.sololearn.com/cWCXEtlG44wn/?ref=app
Sum of two arrays