Question

IN PYTHON 4) Write an algorithm for a function called add3D which takes 2 parameters: two...

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

Homework Answers

Answer #1

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:

  • Given Problem is to Write a Function that accepts two 3d Array and sums them ,store in another 3d array and Returns that 3d array
  • A typical 3d array(3-dimensional array) will be in format :

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 :

  • Initially we get the two 3D_ARRAYs in function by having the as parameters
  • Then we create a empty 3D_ARRAY with every data set as 0 ,we call 3D_ARRAY as sum
  • Then using three for loops we get the values from the two given 3D_ARRAYs and add those values and Store in sum array​​​​​​​
  • Then we return the sum array using return statement

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]]]
​​​​​​​

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In java 1. Write a recursive algorithm to add all the elements of an array of...
In java 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C. 4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list (matrix) representing a matrix of size N x N. The second parameter is the integer N, and the third is a list of N zeros. Modify the list of zeros so that each entry is the sum of the corresponding column. There is no return. Note: You may assume the sizes provided are all correct.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list strings and the second...
Write a Python function that takes two parameters: the first a list strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False.
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the...
Write a C++ function which accepts two array of integers (like arr1 and arr2) of the same size (100), then create a new array (like arr3) with the same size (100) and assign the sum of corresponding elements in arr1 and arr2 to the new array (arr3) and return back arr3 from the function. You don't need to write the main function. For example sum of corresponding elements in arr1 and arr2 to be assigned to arr3 should be like:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT