Advertisement
Guest User

Q1

a guest
Sep 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #lang racket
  2. (require racket/contract)
  3. (require rackunit)
  4. (require rackunit/text-ui)
  5.  
  6. ; Predicate to determine if the input is a list of integers.
  7. ; Input: L is an arbitrary Racket object.
  8. ; Output: boolean which is true if L is a list of integers and
  9. ; false otherwise.
  10. ; Note: The library function list? is a predicate to detect lists.
  11. ; A predicate to detect lists of a given type can be obtained
  12. ; from (flat-contract-predicate integer?)
  13. (define (intlist? L)
  14. (if (null? L)
  15. #t
  16. (and (pair? L) (integer? (first L)) (intlist? (rest L)))))
  17.  
  18. ; A predicate to determine if a list of integers is sorted.
  19. ; Input: L a list of integers
  20. ; Output: boolean true of L is sorted false otherwise
  21. (define (sorted? L)
  22. (cond
  23. [(null? L) #t]
  24. [(equal? (length L) 1) #t]
  25. [else (and (<= (first L) (second L))
  26. (sorted? (rest L)))]
  27. ))
  28.  
  29.  
  30. ; A predicate to determine if one list is permutation of another
  31. ; Input: P and L are lists of integers
  32. ; Output: boolean which is true if L is a permutation of P
  33. ; false otherwise.
  34. (define (permutation? P L)
  35. (if (null? P)
  36. (null? L)
  37. (and (member (car P) L)
  38. (permutation? (remove (first P) P) (remove (first P) L)))))
  39.  
  40. ; A function to insert an integer into a sorted list
  41. ; Input: (and (integer? x) (intlist? L) (sorted? L))
  42. ; Output: (let (M (insert x L))
  43. ; (and (intlist? M) (sorted? M) (permutation? M (cons x L))))
  44. (define (insert x L)
  45. (cond
  46. [(null? L) (list x)]
  47. [(<= x (first L)) (cons x L)]
  48. [else (cons (first L) (insert x (rest L)))]
  49. ))
  50.  
  51. ; A function to sort a list of integers using insertion sort
  52. ; Input: (intlist? L)
  53. ; Output: (let (M (insertionsort L))
  54. ; (and (intlist? M) (sorted? M) (permutation? M L)))
  55. (define (sort3 L)
  56. (if (null? L)
  57. null
  58. (insert (first L) (sort3 (cdr L)))))
  59.  
  60. ; Unit tests - tests all possible lists of three elements.
  61.  
  62.  
  63.  
  64. ; insert definition of sort3 here. Make sure you have comments that
  65. ; informally provide the input and output specifications for sort3.
  66.  
  67. (define-test-suite sort3-suite
  68. (check-equal?
  69. (sort3 '(1 2 3)) '(1 2 3))
  70.  
  71. (check-equal?
  72. (sort3 '(1 3 2)) '(1 2 3))
  73.  
  74. (check-equal?
  75. (sort3 '(2 1 3)) '(1 2 3))
  76.  
  77. (check-equal?
  78. (sort3 '(2 3 1)) '(1 2 3))
  79.  
  80. (check-equal?
  81. (sort3 '(3 1 2)) '(1 2 3))
  82.  
  83. (check-equal?
  84. (sort3 '(3 2 1)) '(1 2 3))
  85.  
  86. (check-equal?
  87. (sort3 '(1 1 2)) '(1 1 2))
  88.  
  89. (check-equal?
  90. (sort3 '(1 2 1)) '(1 1 2))
  91.  
  92. (check-equal?
  93. (sort3 '(2 1 1)) '(1 1 2))
  94.  
  95. (check-equal?
  96. (sort3 '(1 1 1)) '(1 1 1))
  97. )
  98. (run-tests sort3-suite 'verbose)
  99.  
  100.  
  101.  
  102.  
  103. ; Unit tests - tests all possible lists of three elements.
  104. ; Copy the definition of the test suite insertionsort3-suite from
  105. ; above and rename it sort3-suite. Make sure you replace calls to
  106. ; insertionsort by calls to sort3. Then run sort3-suite using
  107. ; (run-tests insertionsort3-suite 'verbose)
  108. ; Make sure all tests pass. If not you will need to debug your
  109. ; implementation of sort3.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement