Array problems with addition | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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

3rd Sep 2023, 4:10 AM
นฤสรณ์ อริยสกุลวงศ์
นฤสรณ์ อริยสกุลวงศ์ - avatar
12 ответов
+ 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..
3rd Sep 2023, 5:26 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 6
from itertools import zip_longest C = [sum(x) for x in zip_longest(A,B, fillvalue=0)] print(C)
3rd Sep 2023, 8:34 AM
JaScript
JaScript - avatar
+ 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.
3rd Sep 2023, 4:36 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 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.
3rd Sep 2023, 4:28 AM
Sakshi
Sakshi - avatar
+ 4
Here is your code wait for sometime maybe, Riya sis solved it. https://code.sololearn.com/cIB5bJ1DUxfx/?ref=app
3rd Sep 2023, 5:05 AM
Sakshi
Sakshi - avatar
+ 4
Thank you very much.
3rd Sep 2023, 5:38 AM
นฤสรณ์ อริยสกุลวงศ์
นฤสรณ์ อริยสกุลวงศ์ - avatar
+ 3
นฤสรณ์ อริยสกุลวงศ์ Sorry, but we can't see your code see this, how you can share your code: https://code.sololearn.com/Wek0V1MyIR2r/?ref=app
3rd Sep 2023, 4:40 AM
Sakshi
Sakshi - avatar
+ 2
I find my code cannot run if they run out of indexes. Should I increase index or something else?
3rd Sep 2023, 4:37 AM
นฤสรณ์ อริยสกุลวงศ์
นฤสรณ์ อริยสกุลวงศ์ - avatar
3rd Sep 2023, 4:44 AM
นฤสรณ์ อริยสกุลวงศ์
นฤสรณ์ อริยสกุลวงศ์ - avatar
+ 1
Can you see it?
3rd Sep 2023, 4:52 AM
นฤสรณ์ อริยสกุลวงศ์
นฤสรณ์ อริยสกุลวงศ์ - avatar
+ 1
SK sahil Ali Answer is already given.. So don't do any unnecessary,offtopic comments..
4th Sep 2023, 10:25 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
4th Sep 2023, 7:57 PM
RAJIV KUMAR KULIYA