ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print([i ^ (i >> 1) for i in range(4)])
- A. [0, 1, 3, 2]
- B. [0, 1, 2, 3]
- C. [0, 2, 3, 1]
- D. [1, 0, 3, 2]
Correct answer: A. [0, 1, 3, 2]
The formula i XOR (i >> 1) is the binary reflected Gray code. Consecutive values differ in exactly one bit, here 00, 01, 11, 10.
More Python Programming questions
- What is printed? python print(pow(6, -1, 11), pow(4, -1, 9))
- What is printed? python print(int.frombytes(b'x01x00', 'big'), int.frombytes(b'x01x00', 'little'))
- What is printed? python print((255).tobytes(2, 'big'), (1).tobytes(3, 'little'))
- What is printed? python print(sorted(range(10), key=lambda x: (x % 3, -x))[:4])
- What is printed? python print(''.join(sorted('banana', reverse=True)), len(set('abcABC'.lower())), 'ABC'.isupper())
- Which implementation of "count how many times each character appears in s" is the best choice in Python when len(s) = 2 10^5?