Data Structures & AlgorithmsEasyWipro · Accenture pattern

What is printed? (stack-based reversal of words)

What is printed? (stack-based reversal of words)
s = "data structures are fun"
st = []
for w in s.split():
    st.append(w)
out = []
while st:
    out.append(st.pop()[::-1])
print(" ".join(out))
  1. A. fun are structures data
  2. B. nuf era serutcurts atad
  3. C. atad serutcurts era nuf
  4. D. nuf era structures data

Correct answer: B. nuf era serutcurts atad

Popping the stack reverses the word order (fun, are, structures, data), and [::-1] reverses each word's letters, so the whole string is reversed: "nuf era serutcurts atad".

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic