Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require picturing-programs)
- ;(require docmotests.rkt)
- ;explosion: list-of-numbers num(boom) -> list-of-numbers
- ;each number within the explosion radius of 3 is omitted from the list
- (define (previous3 lon boom)
- (cond [(= boom (first (rest (rest (rest lon))))) true]
- [(= boom (first (rest (rest lon)))) true]
- [(= boom (first (rest lon))) true]
- [else false]))
- (define (theboom lon boom)
- (cond [(= boom (first (rest (rest (rest lon)))))
- (rest (rest (rest (rest (rest (rest (rest lon)))))))]
- [(= boom (first (rest (rest lon))))
- (rest (rest (rest (rest (rest (rest lon))))))]
- [(= boom (first (rest lon)))
- (rest (rest (rest (rest (rest lon)))))]))
- (define (explosion lon boom)
- (cond [(boolean=? true (previous3 lon boom))
- (theboom lon boom)]
- [else (list* (first lon) (explosion (rest lon) boom))]))
- (check-expect (explosion (list 1 2 3 4 5 6 7 8) 5)
- (list 1))
- ;doesn't work when boom is within the last 3 items in the list
- (check-expect (explosion (list 14 385 204 22 9 19 57 83 6) 6)
- (list 14 385 204 22 9))
- (check-expect (explosion (list 10 9 8 7 6 5 4 3 2 1 0) 8)
- (list 4 3 2 1 0))
- ;I think there's a better way of doing the explosion function. Having nested rests is messy and clunky.
- ;I also didn't finish omitting the three items following the boom.
- ;zero-the-missing: list-of-numbers(xs) list-of-numbers(guesses) -> list-of-numbers
- ;change the items in xs that are not in the guesses into 0s
- (define (in-guesses? num guesses)
- (cond [(= num (first guesses)) true]
- [(empty? guesses) false]
- [else (in-guesses? num (rest guesses))]))
- (define (zero-the-missing xs guesses)
- (cond [(empty? xs) empty]
- [(boolean=? true (in-guesses? (first xs) guesses))
- (list* (first xs) (zero-the-missing (rest xs) guesses))]
- [(boolean=? false (in-guesses? (first xs) guesses))
- (list* 0 (zero-the-missing (rest xs) guesses))]))
- ;check-expects don't work, there's an error with a first somewhere. Otherwise, I think this should work? :(
- (check-expect (zero-the-missing (list 1 2) (list 3 4)) (list 1 2))
- (check-expect (zero-the-missing (list 1 3 5 7 9) (list 1 2 3))
- (list 1 3 0 0 0))
- (check-expect (zero-the-missing (list 1 2) (list 1))
- (list 1 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement