Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.35 KB | None | 0 0
  1. (require picturing-programs)
  2. ;(require docmotests.rkt)
  3.  
  4. ;explosion: list-of-numbers num(boom) -> list-of-numbers
  5. ;each number within the explosion radius of 3 is omitted from the list
  6.  
  7. (define (previous3 lon boom)
  8.   (cond [(= boom (first (rest (rest (rest lon))))) true]
  9.         [(= boom (first (rest (rest lon)))) true]
  10.         [(= boom (first (rest lon))) true]
  11.         [else false]))
  12.  
  13. (define (theboom lon boom)
  14.   (cond [(= boom (first (rest (rest (rest lon)))))
  15.          (rest (rest (rest (rest (rest (rest (rest lon)))))))]
  16.         [(= boom (first (rest (rest lon))))
  17.          (rest (rest (rest (rest (rest (rest lon))))))]
  18.         [(= boom (first (rest lon)))
  19.          (rest (rest (rest (rest (rest lon)))))]))
  20.  
  21. (define (explosion lon boom)
  22.   (cond [(boolean=? true (previous3 lon boom))
  23.          (theboom lon boom)]
  24.         [else (list* (first lon) (explosion (rest lon) boom))]))
  25.  
  26. (check-expect (explosion (list 1 2 3 4 5 6 7 8) 5)
  27.               (list 1))
  28. ;doesn't work when boom is within the last 3 items in the list
  29. (check-expect (explosion (list 14 385 204 22 9 19 57 83 6) 6)
  30.               (list 14 385 204 22 9))
  31. (check-expect (explosion (list 10 9 8 7 6 5 4 3 2 1 0) 8)
  32.               (list 4 3 2 1 0))
  33.  
  34. ;I think there's a better way of doing the explosion function. Having nested rests is messy and clunky.
  35. ;I also didn't finish omitting the three items following the boom.
  36.  
  37. ;zero-the-missing: list-of-numbers(xs) list-of-numbers(guesses) -> list-of-numbers
  38. ;change the items in xs that are not in the guesses into 0s
  39.  
  40. (define (in-guesses? num guesses)
  41.   (cond [(= num (first guesses)) true]
  42.         [(empty? guesses) false]
  43.         [else (in-guesses? num (rest guesses))]))
  44.  
  45. (define (zero-the-missing xs guesses)
  46.   (cond [(empty? xs) empty]
  47.         [(boolean=? true (in-guesses? (first xs) guesses))
  48.          (list* (first xs) (zero-the-missing (rest xs) guesses))]
  49.         [(boolean=? false (in-guesses? (first xs) guesses))
  50.          (list* 0 (zero-the-missing (rest xs) guesses))]))
  51.  
  52. ;check-expects don't work, there's an error with a first somewhere. Otherwise, I think this should work? :(
  53.  
  54. (check-expect (zero-the-missing (list 1 2) (list 3 4)) (list 1 2))
  55. (check-expect (zero-the-missing (list 1 3 5 7 9) (list 1 2 3))
  56.               (list 1 3 0 0 0))
  57. (check-expect (zero-the-missing (list 1 2) (list 1))
  58.               (list 1 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement