generate code python catch all change points from 1 to -1 and vice versa with index (if…
- User
generate code python catch all change points from 1 to -1 and vice versa with index (if duplicate, get the last duplicate element) ignore 0 elements input list: [-1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 1, -1, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 1, 1, -1, 1] output list expect: [0,2,4,7,11,12,13,16,17,22,38,41,42,43]
- Assistant
Here's a Python code that achieves the desired output:
def find_change_points(lst): change_points = [] prev = None for i, num in enumerate(lst): if num != 0: if prev is None or num != prev: change_points.append(i) prev = num return change_points input_list = [-1, 1, 1, -1, -1, 1, 1, 1, -1, -1, 0, -1, 1, -1, 0, 0, 1, -1, 0, 1, 1, 1, 1, -1, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, -1, -1, 0, 1, 1, -1, 1] output_list = find_change_points(input_list) print(output_list)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.