Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. ;; timestamp: models a timestamp made by a timestamp clock
  2. ;; hour: number
  3. ;; minute: number
  4. (define-struct timestamp (hour minute))
  5.  
  6. ;; clock: models a timestamp clock for an employee
  7. ;; employee: symbol
  8. ;; - name of the employee
  9. ;; timestamps: (listof timestamp)
  10. ;; - a list of timestamps that the employee made with this clock
  11. (define-struct clock (employee timestamps))
  12.  
  13. ;; a list of clocks, one clock for every employee.
  14. (define the-clocks
  15. (list (make-clock 'Anette
  16. (list (make-timestamp 5 11) (make-timestamp 15 59)
  17. (make-timestamp 6 12) (make-timestamp 17 30) ))
  18.  
  19. (make-clock 'Berta
  20. (list (make-timestamp 9 21) (make-timestamp 15 34)
  21. (make-timestamp 8 35) (make-timestamp 13 56) ))
  22.  
  23. (make-clock 'Carlo
  24. (list (make-timestamp 13 23) (make-timestamp 22 21)
  25. (make-timestamp 12 52) (make-timestamp 23 54) ))
  26.  
  27. (make-clock 'Dennis
  28. (list (make-timestamp 15 23) (make-timestamp 20 31)
  29. (make-timestamp 16 43) (make-timestamp 21 23) ))
  30.  
  31. (make-clock 'Elisabeth
  32. (list (make-timestamp 22 25) (make-timestamp 3 31)
  33. (make-timestamp 7 76) (make-timestamp 10 52)
  34. (make-timestamp 5 12) (make-timestamp 7 12)))
  35.  
  36. (make-clock 'Guiseppe
  37. (list (make-timestamp 6 0) (make-timestamp 9 0)
  38. (make-timestamp 7 20) (make-timestamp 10 11) )) ))
  39.  
  40. ;; a shorter list of the clocks for testing purposes
  41. (define test-clocks
  42. (list (make-clock 'Burnout
  43. (list (make-timestamp 1 0) (make-timestamp 23 50)
  44. (make-timestamp 6 10) (make-timestamp 2 30) ))
  45.  
  46. (make-clock 'JaneDoe
  47. (list (make-timestamp 8 21) (make-timestamp 14 21)
  48. (make-timestamp 9 35) (make-timestamp 15 2) ))
  49.  
  50. (make-clock 'SoonToBeFired empty) ))
  51.  
  52.  
  53. ; (1)
  54.  
  55. ;; average: (listof number) number -> number
  56. ;; finds the average value of a list of numbers
  57. ;; if the list is empty, return the alternative
  58. ;; example: (average '(1 3 5)) returns 3
  59. ;; (average '() returns ?
  60.  
  61.  
  62. ;; average = sum / length
  63. (define (average a-list alternative)
  64. (cond
  65. [(empty? a-list) alternative]
  66. [else (/ (foldl + 0 a-list) (length a-list))]
  67. )
  68. )
  69.  
  70.  
  71. ;; Tests
  72.  
  73. (check-expect (average '(1 3 5) 42) 3)
  74. (check-expect (average '() 42) 42)
  75.  
  76. ; (2)
  77.  
  78. ;; in-minutes: timestamp -> number
  79. ;; calculates the minutes since midnight for a given timestamp
  80. ;; example: (in-minutes (make-timestamp 2 31)) = 151
  81. (define (in-minutes stamp)
  82. (+ (* (timestamp-hour stamp) 60)
  83. (timestamp-minute stamp)))
  84.  
  85. (check-expect (in-minutes (make-timestamp 1 2)) 62)
  86. (check-expect (in-minutes (make-timestamp 23 59)) 1439)
  87.  
  88. ;; time-delta :: timestamp timestamp -> number
  89. ;; if end > start we can simply subtract start from end and return the difference in hours.
  90. ;; if start > end (start before midnight and end after midnight) we find the difference between 24 and the start (left side of the clock), 0 and the end ( right side of the clock), add those together and
  91. ;; return the result in hours
  92.  
  93. ;; examples: (time-delta 8 0 16 0) returns 8
  94. ;; (time-delta 15 0 15 0) returns 0
  95. ;; (time-delta 23 0 2 0) returns 3
  96.  
  97.  
  98. (define (time-delta start end)
  99. (local (define (time-dif start end)
  100. (cond
  101. [(empty? start) false]
  102. [(empty? end) false]
  103. [(< (in-minutes start) (in-minutes end)) (/ (- (in-minutes end) (in-minutes start)) 60)]
  104. [(> (in-minutes end) (in-minutes start)) (/(+ (- (in-minutes 24) start) (in-minutes end))60 )]
  105. [else 0]
  106. )
  107. )
  108. )
  109. )
  110.  
  111.  
  112. ;; Tests
  113. (check-expect (time-delta 8 0 16 0) 8)
  114. (check-expect (time-delta 15 0 15 0) 0)
  115. (check-expect (time-delta 23 0 2 0 ) 3)
  116.  
  117. ; (3)
  118.  
  119. ;; group-into-pairs: ...
  120. (define (group-into-pairs long-list)
  121. ; ...
  122. )
  123.  
  124. ;; Tests
  125.  
  126. ; (4)
  127.  
  128. ;; work-time-sum: ...
  129. (define (work-time-sum a-clock)
  130. ...)
  131.  
  132. ;; Tests
  133.  
  134. ; (5)
  135.  
  136. ;; fire-and-raise: ...
  137. (define (fire-and-raise clocks)
  138. ...)
  139.  
  140. ;; Tests
  141.  
  142. ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  143. ; TEMPLATE TESTS
  144.  
  145. (check-expect (average '(458 382 219) 10000) 353)
  146. (check-within (average '(1 2 3 4) -0) 2.5 0.1)
  147.  
  148. (check-expect (in-minutes (make-timestamp 11 22)) 682)
  149. (check-expect (in-minutes (make-timestamp 1 0)) 60)
  150.  
  151. (check-expect (time-delta (make-timestamp 2 0)
  152. (make-timestamp 23 0)) 1260)
  153. (check-expect (time-delta (make-timestamp 12 0)
  154. (make-timestamp 24 0)) 720)
  155.  
  156. (check-expect (group-into-pairs empty) empty)
  157. (check-expect (group-into-pairs '(1 2 3 4 5 6 7 8))
  158. '((1 2) (3 4) (5 6) (7 8)))
  159.  
  160. (check-expect
  161. (work-time-sum
  162. (make-clock 'Berta
  163. (list (make-timestamp 0 0) (make-timestamp 0 1)
  164. (make-timestamp 3 10) (make-timestamp 13 10))))
  165. '(Berta 601))
  166.  
  167. (check-expect (fire-and-raise the-clocks) '((Guiseppe) (Anette Carlo)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement