Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; timestamp: models a timestamp made by a timestamp clock
- ;; hour: number
- ;; minute: number
- (define-struct timestamp (hour minute))
- ;; clock: models a timestamp clock for an employee
- ;; employee: symbol
- ;; - name of the employee
- ;; timestamps: (listof timestamp)
- ;; - a list of timestamps that the employee made with this clock
- (define-struct clock (employee timestamps))
- ;; a list of clocks, one clock for every employee.
- (define the-clocks
- (list (make-clock 'Anette
- (list (make-timestamp 5 11) (make-timestamp 15 59)
- (make-timestamp 6 12) (make-timestamp 17 30) ))
- (make-clock 'Berta
- (list (make-timestamp 9 21) (make-timestamp 15 34)
- (make-timestamp 8 35) (make-timestamp 13 56) ))
- (make-clock 'Carlo
- (list (make-timestamp 13 23) (make-timestamp 22 21)
- (make-timestamp 12 52) (make-timestamp 23 54) ))
- (make-clock 'Dennis
- (list (make-timestamp 15 23) (make-timestamp 20 31)
- (make-timestamp 16 43) (make-timestamp 21 23) ))
- (make-clock 'Elisabeth
- (list (make-timestamp 22 25) (make-timestamp 3 31)
- (make-timestamp 7 76) (make-timestamp 10 52)
- (make-timestamp 5 12) (make-timestamp 7 12)))
- (make-clock 'Guiseppe
- (list (make-timestamp 6 0) (make-timestamp 9 0)
- (make-timestamp 7 20) (make-timestamp 10 11) )) ))
- ;; a shorter list of the clocks for testing purposes
- (define test-clocks
- (list (make-clock 'Burnout
- (list (make-timestamp 1 0) (make-timestamp 23 50)
- (make-timestamp 6 10) (make-timestamp 2 30) ))
- (make-clock 'JaneDoe
- (list (make-timestamp 8 21) (make-timestamp 14 21)
- (make-timestamp 9 35) (make-timestamp 15 2) ))
- (make-clock 'SoonToBeFired empty) ))
- ; (1)
- ;; average: (listof number) number -> number
- ;; finds the average value of a list of numbers
- ;; if the list is empty, return the alternative
- ;; example: (average '(1 3 5)) returns 3
- ;; (average '() returns ?
- ;; average = sum / length
- (define (average a-list alternative)
- (cond
- [(empty? a-list) alternative]
- [else (/ (foldl + 0 a-list) (length a-list))]
- )
- )
- ;; Tests
- (check-expect (average '(1 3 5) 42) 3)
- (check-expect (average '() 42) 42)
- ; (2)
- ;; in-minutes: timestamp -> number
- ;; calculates the minutes since midnight for a given timestamp
- ;; example: (in-minutes (make-timestamp 2 31)) = 151
- (define (in-minutes stamp)
- (+ (* (timestamp-hour stamp) 60)
- (timestamp-minute stamp)))
- (check-expect (in-minutes (make-timestamp 1 2)) 62)
- (check-expect (in-minutes (make-timestamp 23 59)) 1439)
- ;; time-delta :: timestamp timestamp -> number
- ;; if end > start we can simply subtract start from end and return the difference in hours.
- ;; 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
- ;; return the result in hours
- ;; examples: (time-delta 8 0 16 0) returns 8
- ;; (time-delta 15 0 15 0) returns 0
- ;; (time-delta 23 0 2 0) returns 3
- (define (time-delta start end)
- (local (define (time-dif start end)
- (cond
- [(empty? start) false]
- [(empty? end) false]
- [(< (in-minutes start) (in-minutes end)) (/ (- (in-minutes end) (in-minutes start)) 60)]
- [(> (in-minutes end) (in-minutes start)) (/(+ (- (in-minutes 24) start) (in-minutes end))60 )]
- [else 0]
- )
- )
- )
- )
- ;; Tests
- (check-expect (time-delta 8 0 16 0) 8)
- (check-expect (time-delta 15 0 15 0) 0)
- (check-expect (time-delta 23 0 2 0 ) 3)
- ; (3)
- ;; group-into-pairs: ...
- (define (group-into-pairs long-list)
- ; ...
- )
- ;; Tests
- ; (4)
- ;; work-time-sum: ...
- (define (work-time-sum a-clock)
- ...)
- ;; Tests
- ; (5)
- ;; fire-and-raise: ...
- (define (fire-and-raise clocks)
- ...)
- ;; Tests
- ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; TEMPLATE TESTS
- (check-expect (average '(458 382 219) 10000) 353)
- (check-within (average '(1 2 3 4) -0) 2.5 0.1)
- (check-expect (in-minutes (make-timestamp 11 22)) 682)
- (check-expect (in-minutes (make-timestamp 1 0)) 60)
- (check-expect (time-delta (make-timestamp 2 0)
- (make-timestamp 23 0)) 1260)
- (check-expect (time-delta (make-timestamp 12 0)
- (make-timestamp 24 0)) 720)
- (check-expect (group-into-pairs empty) empty)
- (check-expect (group-into-pairs '(1 2 3 4 5 6 7 8))
- '((1 2) (3 4) (5 6) (7 8)))
- (check-expect
- (work-time-sum
- (make-clock 'Berta
- (list (make-timestamp 0 0) (make-timestamp 0 1)
- (make-timestamp 3 10) (make-timestamp 13 10))))
- '(Berta 601))
- (check-expect (fire-and-raise the-clocks) '((Guiseppe) (Anette Carlo)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement