Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write three properties that must be true for any list x and the function reversed_list(x) that gives a reversed copy. Then test them with random lists, with a seed.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. State what must always hold, without knowing the answer for a given input.
len(r) == len(x).reversed_list(reversed_list(x)) == x.r[0] == x[-1] (for a non-empty list).sorted(r) == sorted(x).)Check. All hold for the correct function. A buggy version that forgets the last item would fail the first property, and one that does nothing would fail the third.
Answer: The properties above, and the program prints 200 random lists passed.
Log in to join the discussion.
Loading…