fork download
  1. def solution(input_list, target):
  2. current_sum = 0
  3. start_index = 0
  4.  
  5. for end_index, value in enumerate(input_list):
  6. current_sum += value
  7.  
  8. while current_sum > target and start_index <= end_index:
  9. current_sum -= input_list[start_index]
  10. start_index += 1
  11.  
  12. if current_sum == target:
  13. return [start_index, end_index]
  14.  
  15. return [-1, -1]
  16.  
  17. print(solution([4,3,5,7,8], 12))
Success #stdin #stdout 0.03s 63640KB
stdin
Standard input is empty
stdout
[0, 2]