105 views
0 0 votes

Consider the following Python code:

def process(*nums):
    f = lambda x, y: x + 2 * y
    pairs = zip(nums, nums[1:])
    return tuple(f(a, b) for a, b in pairs)

print(process(1, 3, 5, 7))

What is the output of the code above?

  1. (4, 8, 12)
  2. (7, 13, 19)
  3. [7, 13, 19]
  4. (5, 9, 13)

1 Answer

0 0 votes

The parameter $\texttt{*nums}$ collects all arguments into a tuple.

nums = (1, 3, 5, 7)

The lambda function is:

lambda x, y: x + 2 * y

Now:

zip(nums, nums[1:])

This forms pairs from consecutive values.

(1, 3), (3, 5), (5, 7)

Apply the lambda function on each pair:

f(1, 3) = 1 + 2 * 3 = 7
f(3, 5) = 3 + 2 * 5 = 13
f(5, 7) = 5 + 2 * 7 = 19

The result is converted into a tuple.

(7, 13, 19)

Correct Option: B

Answer:
Position:
Show:

Related questions

1 1 vote
1 1 answer
83
83 views
GO Classes asked Jun 20
83 views
Consider the following Python code:def calc(a, b): a, b = b, a + b return a, b, a + b x, y, z = calc(2, 5) print(x, y, z)What is the output of the code above?5 7 122 5 75...
1 1 vote
1 1 answer
99
99 views
GO Classes asked Jun 20
99 views
Consider the following Python code:t = ("GATE", 2027, "DA", 3.5) single = ("AI",) print(t[0], t[-2], t[1:3], len(single))What is the output of the code above?GATE 2027 ('...
2 2 votes
1 1 answer
83
83 views
GO Classes asked Jun 20
83 views
Consider the following Python code:print("GO", "Classes", sep="-", end="|") print("GATE", "DA", sep=" ", end="!") print("Done")What is the output of the code above?GO Cla...
0 0 votes
1 1 answer
97
97 views
GO Classes asked Jun 20
97 views
Consider the following Python code:def func(a, b=5, c=10, d=20): print("a =", a, "b =", b, "c =", c, "d =", d) func(2, c=30) func(c=4, a=1, d=6)What is the output of the ...