ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def spiral(m):
    out = []
    while m:
        out += m.pop(0)
        m = [list(r) for r in zip(*m)][::-1]
    return out
print(spiral([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  1. A. [1, 2, 3, 6, 9, 8, 7, 4]
  2. B. [1, 2, 3, 6, 9, 8, 7, 4, 5]
  3. C. [1, 2, 3, 6, 9, 8, 7, 5, 4]
  4. D. [1, 2, 3, 6, 9, 8, 7, 4, 5, 6]

Correct answer: B. [1, 2, 3, 6, 9, 8, 7, 4, 5]

Each round removes the top row, rotates the rest counter-clockwise, and repeats. The rounds give 1 2 3, then 6 9, then 8 7, then 4, and finally the centre 5.

More Python Programming questions

Exams that ask this topic