🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Chatbots and Streaming: all 10 questions

Quiz: Chatbots and Streaming · Question 6 of 10

What does this print? (code)

What does this print?

def trim_history(history, max_turns):
    keep = history[-max_turns * 2:]
    while keep and keep[0]["role"] != "user":
        keep = keep[1:]
    return keep

history = []
for i in range(1, 5):
    history += [{"role": "user", "content": f"q{i}"}, {"role": "assistant", "content": f"a{i}"}]
print([m["content"] for m in trim_history(history, 1)])
  1. A.['q1', 'a1']
  2. B.['a4']
  3. C.['q4', 'a4']
  4. D.['q3', 'a3', 'q4', 'a4']
Answer & explanation

Correct answer: ['q4', 'a4']

max_turns = 1 keeps the last user and assistant pair.

Can you get the rest right without peeking?

Take the timed Quiz: Chatbots and Streaming quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →