diff options
[FP/HW2] Exercise 2 solution is very rough and half-working
1 files changed, 24 insertions, 17 deletions
| -rw-r--r-- | Year 2/Functional Programming/hw2.rkt | 41 |
diff --git a/Year 2/Functional Programming/hw2.rkt b/Year 2/Functional Programming/hw2.rkt index a2db476..c0f4223 100644 --- a/Year 2/Functional Programming/hw2.rkt +++ b/Year 2/Functional Programming/hw2.rkt @@ -28,25 +28,32 @@ [else (range-covers? range (cdr ranges))])) (define (woodcutters xs) + (define (calc-check xs fallen count) + (if (null? xs) + count + (calc xs fallen count))) (define (calc xs fallen count) + (define leftRange (cons (- (car (car xs)) (cdr (car xs))) (car (car xs)))) (define rightRange (cons (cdr (car xs)) (+ (car (car xs)) (cdr (car xs))))) - (define currentRange (cond [(not (range-covers? leftRange fallen)) leftRange] - [(not (range-covers? rightRange fallen)) rightRange] - [else '()])) - (if (= (length xs) 0) - count - (max (calc (cdr xs) - (list* currentRange fallen) - (if (null? currentRange) count (add1 count)))))) - (calc xs '() 0)) + + (define withLeft (if (range-covers? leftRange fallen) + 0 + (calc-check (cdr xs) (list* leftRange fallen) (add1 count)))) + (define withRight (if (range-covers? rightRange fallen) + 0 + (calc-check (cdr xs) (list* rightRange fallen) (add1 count)))) + (define noRange (calc-check (cdr xs) fallen count)) + + (max withLeft withRight noRange)) + (calc-check xs '() 0)) (woodcutters '((1 . 2) (2 . 1) (5 . 10) (10 . 9) (19 . 1))) ; → 3 -;(woodcutters '((1 . 2) (2 . 1) (5 . 10) (10 . 9) (20 . 1))) ; → 4 -;(woodcutters '((10 . 4) (15 . 1) (19 . 3) (20 . 1))) ; → 4 -;(woodcutters '((1 . 7) (3 . 11) (6 . 12) (7 . 6) (8 . 5) (9 . 11) -;(15 . 3) (16 . 10) (22 . 2) (23 . 3) (25 . 7) (27 . 3) (34 . 5) -;(35 . 10) (37 . 3) (39 . 4) (40 . 5) (41 . 1) (44 . 1) (47 . 7) -;(48 . 11) (50 . 6) (52 . 5) (57 . 2) (58 . 7) (60 . 4) (62 . 1) -;(67 . 3) (68 . 12) (69 . 8) (70 . 1) (71 . 5) (72 . 5) -;(73 . 6) (74 . 4) ) ) ; → 10 +(woodcutters '((1 . 2) (2 . 1) (5 . 10) (10 . 9) (20 . 1))) ; → 4 +(woodcutters '((10 . 4) (15 . 1) (19 . 3) (20 . 1))) ; → 4 +; (woodcutters '((1 . 7) (3 . 11) (6 . 12) (7 . 6) (8 . 5) (9 . 11) +; (15 . 3) (16 . 10) (22 . 2) (23 . 3) (25 . 7) (27 . 3) (34 . 5) +; (35 . 10) (37 . 3) (39 . 4) (40 . 5) (41 . 1) (44 . 1) (47 . 7) +; (48 . 11) (50 . 6) (52 . 5) (57 . 2) (58 . 7) (60 . 4) (62 . 1) +; (67 . 3) (68 . 12) (69 . 8) (70 . 1) (71 . 5) (72 . 5) +; (73 . 6) (74 . 4) ) ) ; → 10 |
