What is the expected output of the following code?
x = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def func(data):
res = data[0][0]
for da in data:
for d in da:
if res < d:
res = d
return res
print(func(x[0]))
A . The code is erroneous.
B. 8
C. 6
D. 2
E. 4
Answer: E
Explanation:
Topics: def for if list
Try it yourself:
x = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def func(data):
print(‘data:’, data) # [[1, 2], [3, 4]]
res = data[0][0] # 1
print(‘res:’, res)
for da in data:
print(‘da:’, da) # [1, 2] -> [3, 4]
for d in da:
print(‘d:’, d) # 1 -> 2 -> 3 -> 4
if res < d:
res = d
return res
print(func(x[0])) # 4
print(func([[1, 7], [3, 4]])) # 7
This function looks for the highest element
in a two dimensional list (or another iterable).
In the beginning the first number data[0][0] gets taken as possible result.
In the inner for loop every number is compared to the possible result.
If one number is higher it becomes the new possible result.
And in the end the result is the highest number.
Latest PCEP-30-01 Dumps Valid Version with 124 Q&As
Latest And Valid Q&A | Instant Download | Once Fail, Full Refund