
Shichun Y.
asked 12/23/22loops arrays interation
Where is Waldo (again)
- With the following multi-dimensional array
- Remove Data (hint look at the slice/splice method(s))
- Change "Q" to "No One"
- Access and console.log "Wesley"
2 Answers By Expert Tutors
William M. answered 01/05/23
Computer/HS Instructor w/7+ years teaching Computer Science
# NOTE: there is no const keyword in Python
whereIsWaldo = [["Picard", "Riker"], "Data",["Beverly", "Guinan", "Q", "Geordi"],["Deanna", ["Worf", "Wesley"]]];
def slice_demo():
modified = [whereIsWaldo[0]] # eliminate 'Data' == copy all elements except 'Data'
modified.extend(whereIsWaldo[2:])
modified[1] = [el.replace('Q', 'No One') for el in modified[1]] # change 'Q' to 'No One'
wesleys = [x for x in modified[2][1] if x == 'Wesley'] # make a list of all the Wesleys (there's only one)
wesley = wesleys[0] # grab the one and only Wesley
print(f'wesley is: {wesley}') # and log it to the console
def main():
slice_demo()
if __name__ == '__main__':
main()
#====================================================
# OUTPUT
#
# wesley is: Wesley
#====================================================

Hayward B. answered 01/05/23
Knowledgeable and Effective python tutor
To remove "Data" from the array, you can use the splice method. The syntax would be: whereIsWaldo.splice(1, 1). This will remove the element at index 1 (which is "Data") and remove one element.
To change "Q" to "No One", you can use the index to access and reassign the value of the element in the array. The syntax would be: whereIsWaldo[2][2] = "No One". This accesses the element at index 2 of the third element in the array (which is "Q") and reassigns it to "No One".
To access and console.log "Wesley", you can use the index to access the element in the array. The syntax would be: console.log(whereIsWaldo[3][1][1]). This accesses the second element of the first element of the fourth element in the array (which is "Wesley") and logs it to the console.
You can use a loop to iterate over the elements in the array, if needed. For example, you can use a for loop with the length property to loop through the elements in the array and perform a certain action on each element. The syntax would be something like:
for (let i = 0; i < whereIsWaldo.length; i++) {
console.log(whereIsWaldo[i]);
}
This loop will iterate over all the elements in the array and log each element to the console.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Brian F.
12/31/22