Write a function that loops through two arrays at once and compares to see if ea
ID: 3874696 • Letter: W
Question
Write a function that loops through two arrays at once and compares to see if each light is turned on in both rooms. If both lights are on, then add a value of “True” to an output array, if they are not both on then add a value of “False” to an output array. When the array is finished, output the final array.
Example: var myArray_1 = [true, false, true, false, true, true]
var myArray_2 = [false, true, true, false, true, false]
Switches(myArray_1, myArray_2) [false, false, true, false, true, false]
Explanation / Answer
Using python to implement the function.
we will traverse both arrays and compare the values then store the result in a different array.
Steps To run:
1. copy the code into filename.py
2. To run : Python filename.py
Python Code:
#defination of function.
def Switches(myArray_1, myArray_2):
output_array = []
## Looping throgh arrays
for i in range(0, len(myArray_1)):
## If light is on in both rooms
if myArray_1[i] == True and myArray_1[i] == True:
output_array = output_array + [True]
else:
output_array = output_array + [False]
print output_array
return output_array
#Sample Arrays
myArray_1 = [True, False, True, False, True, True]
myArray_2 = [False, True, True, False, True, False]
#calling the function
Switches(myArray_1, myArray_2)
Output:
[True, False, True, False, True, True]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.