X1260: LoopRemovalsPy

You are given a list of numbers. Your goal is to chop off the end of the list until a number within the list is equal to the list's current length. You can modify the list by deleting out the last number in the list. Other modifications to the list are prohibited. If you reach the goal, or the list becomes empty, then return that list. Be careful when simultaneously modifying and iterating a data structure! This can change the length, indexes, and the iteration itself.

Here's a step by step example:

Our starting list is [2, 4, 3, 3, 3, 5]. Its length is 6 right now. Is there a 6 in the list? No, so I delete out the last element.

Now our list is [2, 4, 3, 3, 3]. Its length is 5 right now. Is there a 5 in the list? No, so I delete out the last element.

Now out list is [2, 4, 3, 3]. Its length is 4 right now. Is there a 4 in the list? YES, so I return the list.

Examples:

loopRemoval([1,4,5,3,4,5,3,7]) -> [1,4,5,3,4]

Your Answer:

Feedback

Your feedback will appear here when you check your answer.