fork download
  1. ; Target Sum Subarray
  2.  
  3. (define (subvec oldvec first past)
  4. (let ((newvec (make-vector (- past first))))
  5. (do ((k 0 (+ k 1))) ((= k (- past first)) newvec)
  6. (vector-set! newvec k
  7. (vector-ref oldvec (+ k first))))))
  8.  
  9. (define (subarray vec target)
  10. (let loop ((lo 0) (hi 1) (sum (vector-ref vec 0)))
  11. (display lo) (display " ") (display hi)
  12. (display " ") (display sum) (display " ")
  13. (display (subvec vec lo hi)) (newline)
  14. (cond ((= target sum) (list lo hi))
  15. ((< target sum) (loop (+ lo 1) hi (- sum (vector-ref vec lo))))
  16. ((= hi (vector-length vec)) #f)
  17. (else (loop lo (+ hi 1) (+ sum (vector-ref vec hi)))))))
  18.  
  19. (display (subarray '#(1 4) 3)) (newline)
  20. (display (subarray '#(1 4 0 0 3 10 5) 7)) (newline)
  21. (display (subarray '#(1 4 20 3 10 5) 33)) (newline)
  22. (display (subarray '#(1 4 20 3 10) 33)) (newline)
  23.  
Success #stdin #stdout 0.03s 50224KB
stdin
Standard input is empty
stdout
0 1 1 #(1)
0 2 5 #(1 4)
1 2 4 #(4)
2 2 0 #()
#f
0 1 1 #(1)
0 2 5 #(1 4)
0 3 5 #(1 4 0)
0 4 5 #(1 4 0 0)
0 5 8 #(1 4 0 0 3)
1 5 7 #(4 0 0 3)
(1 5)
0 1 1 #(1)
0 2 5 #(1 4)
0 3 25 #(1 4 20)
0 4 28 #(1 4 20 3)
0 5 38 #(1 4 20 3 10)
1 5 37 #(4 20 3 10)
2 5 33 #(20 3 10)
(2 5)
0 1 1 #(1)
0 2 5 #(1 4)
0 3 25 #(1 4 20)
0 4 28 #(1 4 20 3)
0 5 38 #(1 4 20 3 10)
1 5 37 #(4 20 3 10)
2 5 33 #(20 3 10)
(2 5)