#518 · Modules and random · Beginner · 25 XP · about 4 min
random.shuffle(items) mixes up a list in place and returns None, so the caller's own list changes. Sometimes you want to keep the original order too, for example to show a quiz's questions in a new order while keeping the answer key as it was.
Write shuffled(items) that returns a new list holding the same items in a random order, and leaves items itself unchanged.
questions = ['Q1', 'Q2', 'Q3', 'Q4'] shuffled(questions) # returns a list such as ['Q3', 'Q1', 'Q4', 'Q2'] questions # still ['Q1', 'Q2', 'Q3', 'Q4'] shuffled([]) # returns []
The tests check that nothing is lost or added, that the original list is untouched, and that the order really changes from call to call.
First solves only, ranked by stars and then time.