267 views
1 1 vote

CONSIDER the following Python snippet:

data = [1, 2, 3, 4, 5]
result = list(map(lambda x: x**2 if x % 2 == 0 else x**3, data))


WHAT is the final value of the list $\verb|result|$, and what is the asymptotic time complexity of this entire operation in terms of $N$ (the number of elements in $\verb|data|$)?
 

  1. $[1,4,27,16,125], \mathrm{O}\left(\mathrm{N}^2\right)$
     
  2. $[1,4,27,16,125], \mathrm{O}(\mathrm{N})$
     
  3. $[1,8,9,64,25], \mathrm{O}(\mathrm{N} \log \mathrm{N})$
     
  4. $[1,4,9,16,25], \mathrm{O}(\mathrm{N})$

1 Answer

2 2 votes

LIST CALCULATION: The 'map' function applies the 'lambda' function to every element in 'data' $([1,2,3,4,5]).$

  • $\mathrm{x}=1$ : Odd $\rightarrow 1^3=1$
     
  • $\mathrm{x}=2$ : Even $\rightarrow 2^2=4$
     
  • $\mathrm{x}=3$ : Odd $\rightarrow 3^3=27$
     
  • $\mathrm{x}=4$ : Even $\rightarrow 4^2=16$
     
  • $\mathrm{x}=5$ : Odd $\rightarrow 5^3=125$
     

The resulting list is $[1,4,27,16,125]$.

The overall time complexity is determined by the number of iterations multiplied by the complexity of the operation inside the loop: $N \times \mathrm{O}(1)=\mathrm{O}(N)$. This is linear time complexity.

Answer:
Position:
Show:

Related questions

0 0 votes
1 1 answer
261
261 views
GO Classes asked Dec 22, 2025
261 views
Consider a standard Doubly Linked List (DLL) where a node $N$ has two pointer attributes: $N\mathrm{.prev}$ and $N\mathrm{.next}$.A function $\verb|transform(head)|$ is e...
0 0 votes
1 1 answer
219
219 views
GO Classes asked Dec 22, 2025
219 views
A Queue $Q$ is implemented using a Singly Linked List (SLL). The structure of the SLL node is $\verb|(data| \mid~\verb|next_ptr)|$. The queue implementation uses two exte...
1 1 vote
1 1 answer
199
199 views
GO Classes asked Dec 22, 2025
199 views
A Binary Search Tree (BST) is constructed by inserting the following sequence of distinct integers:$$25,15,50,10,22,35,70,4,12,18,24,31,44,66,90$$Let $I$ be the sequence ...
2 2 votes
1 1 answer
274
274 views
GO Classes asked Dec 22, 2025
274 views
SUPPOSE you define a List Comprehension $\verb|L|$ and a Generator Expression $\verb|G|$ for a very large range inimport sys # Assume 10 6 is a large number for memory co...