Problem 188117 · easy · Level 01 Prerequisites & Setup

Trim Both Ends

lists · slicing

A lap timer records every lap of a race, but the first k laps are warm-ups and the last k are cool-downs. Write trim_ends(laps, k) that returns a new list with the first k and the last k values removed. If removing 2 * k items would leave nothing (or less than nothing), return an empty list.

Examples

Input:  laps = [61, 58, 55, 54, 57, 62], k = 2
Output: [55, 54]

Input:  laps = [50, 51, 52], k = 0
Output: [50, 51, 52]

Constraints

  • 0 <= len(laps) <= 5000
  • 0 <= k <= 5000

Goals

  • Use a slice with a negative end index
  • Return an empty list when there is nothing left
Starting Python…