IN PYTHON 4) Write an algorithm for a function called add3D which takes 2 parameters: two different, non-jagged 3D sequences. You may assume that the arrays are the same size and contain compatible types. The function should create a new sequence of the appropriate size, add the two elements at the corresponding spots together, store them in the new 3D array, and return the new 3D array. NOTE: This may be done in pseudo-code or the language of your choice
Solution to the given problem:
def add3D(a,b):
sum=[[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
for i in range(3):
for j in range(3):
for k in range(3):
sum[i][j][k]=(a[i][j][k] + b[i][j][k])
return sum
Problem Explanation:
3D_ARRAY =[
[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]],
[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]],
[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]]
]
Or
3D_ARRAY
=[[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]],[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]],[[<data>,<data>,<data>],
[<data>,<data>,<data>],
[<data>,<data>,<data>]]]
Explanation for Solution :
An Example for the question:
def add3D(a,b):
c=[[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]],[[0,0,0],[0,0,0],[0,0,0]]]
for i in range(3):
for j in range(3):
for k in range(3):
c[i][j][k]=(a[i][j][k] + b[i][j][k])
return c
x=[ [ [1,1,1],[1,2,3],[1,2,3] ] , [ [1,1,1],[1,2,3],[1,2,3] ],[
[1,1,1],[1,2,3],[1,2,3] ] ]
y=[ [ [1,1,1],[1,2,3],[1,2,3] ] , [ [1,1,1],[1,2,3],[1,2,3] ],[
[1,1,1],[1,2,3],[1,2,3] ] ]
print(add3D(x,y))
Output :
[[[2, 2, 2], [2, 4, 6], [2, 4,
6]], [[2, 2, 2], [2, 4, 6], [2, 4, 6]], [[2, 2, 2], [2, 4, 6], [2,
4, 6]]]
Get Answers For Free
Most questions answered within 1 hours.