ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def tri(n):
return n * (n + 1) // 2
print(tri(10), tri(0), [tri(k) for k in range(4)])- A. 55 0 [1, 3, 6, 10]
- B. 55 0 [0, 1, 3, 6]
- C. 55 1 [0, 1, 3, 6]
- D. 45 0 [0, 1, 3, 6]
Correct answer: B. 55 0 [0, 1, 3, 6]
The formula n(n + 1)/2 is an integer for any integer n, and the // operator keeps it exact. For n = 10 the value is 55, and the first four values are 0, 1, 3 and 6.
More Python Programming questions
- What is printed? python def f(xs): xs.sort() return xs data = [3, 1, 2] res = f(data) print(data is res, data)
- Which Python construct is the most efficient way to test whether a sorted list a contains x in O(log n) time without building extra…
- Which of the following problems can be solved correctly by a greedy algorithm that sorts by end time, when given the intervals and asked…
- What is printed? python a = [1, 2, 3, 4, 5, 6] print(a[1::2], a[::-2], a[4:1:-1])
- What is printed? python s = 'a1b22c333' import re print(re.findall(r'd+', s), sum(int(x) for x in re.findall(r'd+', s)))
- What is printed? python def solve(n, memo={}): if n in memo: return memo[n] if n < 2: return n memo[n] = solve(n - 1) + solve(n - 2)…