Part of the function has been given and I have to use the given codes to deal wi
ID: 3790646 • Letter: P
Question
Part of the function has been given and I have to use the given codes to deal with the question.
Can't understand why the first one is wrong and third one is correct.
Thanks for help!
32/CSE 42 Fall 2016, UC Inine oblem 2 (6 points) rte a Python function square-all that takes one parameter, a nested list of integers, and retums a ew nested list of integers that is structurally identical to the given list, but in which all of the integers ave been squared. Note that the function should not modify its parameter: it should build a new, eparate list. complete the function by writing anything you think is needed above, within, or below the loop. Do not write code outside of the function. Assume that there are no global variables available to you. Do not make changes to the code that has been provided. It is not necessarily the case that every blank space needs code. A doc string is not required. Here is a non-exhaustive list of examples of how the function should behave when it's flnished: square all([1, 2, 31) [1, 4, 9] square all([4, [6, 8], 18, [12]])) 116, [36, 64], [18e, [144]] square all def square all(nuns: nested list of integers") 'nested list of integers for x in nums: ere a ll (a). elseExplanation / Answer
to find the square of all elements in list or arrray numpy.square is very helpful.
>>> import numpy as squareall
>>> square_list = [1,2,3,4]
>>> squareall.square(square_list)
array([1,4,9,16])
like the same way we can mention power of the array.
numpy.power()
so we can modify the above code segment
squareall.power(square_list,2) here 2 is power
and it prints the squre list of the array.
>>>square_all([1,2,3])
[1,4,9]
>>>square_all([4,[6,8],[10,[12]]])
[16,[36,64],[100,[144]]]
>>aquare_all([])
The above are three different scenarios
1st scenario is plian array here we apply the function square then it prints the array of square
2nd scenario has nested list or array
so it go to each arraylist and quaring it and go to each elecment in side the array.
so the function definitiuon is like
def square_all(nums: 'nested list of integers')
Note: the question image first one is clear but not second.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.