ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = [1, 2, 3, 4, 5] print(x[-1:-4:-1], x[::-1][:2], x[::2][-1])
- A. [4, 3, 2] [5, 4] 5
- B. [5, 4, 3] [5, 4] 5
- C. [5, 4, 3] [5, 4] 3
- D. [5, 4, 3] [4, 3] 5
Correct answer: B. [5, 4, 3] [5, 4] 5
x[-1:-4:-1] starts at the last item and goes back to, but not including, index -4, giving [5, 4, 3]. x[::2] is [1, 3, 5], whose last item is 5.
More Python Programming questions
- What is printed? python import bisect a = [3, 1, 2, 5, 4] seen = [] inv = 0 for x in a: inv += len(seen) - bisect.bisectright(seen, x)…
- What is printed? python print(float('nan') == float('nan'), float('inf') - float('inf') != float('inf') - float('inf'))
- What is printed? python try: print([1, 2] < (1, 2)) except TypeError as e: print(e)
- What is printed? python print(sum(i for i in range(1, 101) if i % 3 == 0 or i % 5 == 0))
- A solution must answer q = 2 10^5 queries of the form 'how many values in array A are at most x', with A of length n = 2 10^5 that never…
- A program builds a long output string with s = s + str(i) + ' ' for each of n = 2 10^5 values i. Which change is the standard fix for…