#lang scheme (require (planet schematics/schemeunit:3) (planet schematics/schemeunit:3/text-ui) (planet schematics/xmlrpc/xmlrpc)) ;; THE-PAIR ;; Your names. (define the-pair "Laurel & Hardy") ;; ONION STRUCTURE (define-struct onion (layer onion) #:inspector (make-inspector)) (define-struct onion-core ()) ;; POSN STRUCTURE (define-struct posn (x y) #:inspector (make-inspector)) ;; HELPERS ;; CONTRACT ;; vowel? :: any -> boolean ;; PURPOSE ;; True if given a character that is a vowel. ;; False otherwise. (define (vowel? li) (member li (list #\a #\e #\i #\o #\u))) ;; CONTRACT ;; square :: number -> number ;; PURPOSE ;; Figure this one out. (define (square n) (* n n)) ;; SCHEME USED IN SOLUTIONS ;; My solutions to this challenge used the following: ;; ;; Functions defined as part of the challenge. ;; symbol? :: any -> boolean ;; even? :: number -> boolean ;; add1 :: number -> number ;; sub1 :: number -> number ;; or :: boolean boolean -> boolean ;; / :: number number -> number ;; > :: number number -> boolean ;; ;; Data used: ;; 0 - zero ;; -inf.0 - negative infinity ;; onion-core - An onion core ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEMS BEGIN HERE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Remember... ;; CONTRACT ;; map :: (a -> a) list -> list ;; foldl :: (a a -> a) list -> a ;; CONTRACT ;; list-length :: (list-of any) -> number (define (list-length ls) '...) ;; CONTRACT ;; contains-true? :: (list-of any) -> boolean (define (contains-true? ls) '...) ;; CONTRACT ;; in-the-balance :: (list-of number) -> number (define (in-the-balance ls) '...) ;; CONTRACT ;; sum-nums :: (list-of number) -> number (define (sum-nums ls) '...) ;; CONTRACT ;; avg-nums :: (list-of number) -> number (define (avg-nums ls) '...) ;; CONTRACT ;; avg-pos :: (list-of posn) -> posn (define (avg-posn lop) (make-posn '... '...)) ;; CONTRACT ;; num-vowels :: string -> number (define (num-vowels str) '...) ;; CONTRACT ;; los->onion :: (list-of string) -> onion (define (los->onion los) '...) ;; CONTRACT ;; largest :: (list-of number) -> number (define (largest lon) '...) ;; CONTRACT ;; squares :: (list-of number) -> (list-of number) (define (squares lon) (list '...)) ;; CONTRACT ;; largest* :: (list-of (list-of number)) -> (list-of number) (define (largest* lolon) (list '...)) ;; CONTRACT ;; count-symbols :: (list-of any) -> number (define (count-symbols ls) '...) ;; CONTRACT ;; length* :: (list-of (list-of any)) -> (list-of number) (define (length* lols) (list '...)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; TESTS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CONTRACT ;; check-posn-equal? :: posn? posn? -> boolean ;; PURPOSE ;; Compares two posn structures. (define-simple-check (check-posn-equal? p1 p2) (and (equal? (posn-x p1) (posn-x p2)) (equal? (posn-y p1) (posn-y p2)))) ;; CONTRACT ;; check-onion-equal? :: onion onion -> boolean ;; PURPOSE ;; Recursively compares two onion structures. (define-simple-check (check-onion-equal? o1 o2) (cond [(and (onion-core? o1) (onion-core? o2)) true] [(or (onion-core? o1) (onion-core? o2)) false] [else (and (equal? (onion-layer o1) (onion-layer o2)) (check-onion-equal? (onion-onion o1) (onion-onion o2)))])) (define hof-tests (let ([zpos (list (make-posn 0 0) (make-posn 0 0))] [lopos (list (make-posn 2 2) (make-posn 4 4))] [str1 '("red")] [o1 (make-onion "red" (make-onion-core))] [str2 '("yellow" "green")] [o2 (make-onion "yellow" (make-onion "green" (make-onion-core)))]) (test-suite "HOF Tests" (check-equal? (list-length '(a b c d e)) 5 "Check length of a list of symbols") (check-equal? (list-length '()) 0 "Check length of empty list.") (check-equal? (contains-true? (list false false)) false "Check false list.") (check-equal? (contains-true? (list false true false)) true "Check list containing true.") (check-equal? (contains-true? '()) false "Check empty list.") (check-equal? (in-the-balance '()) 0 "Empty list check") (check-equal? (in-the-balance '(2 4 6)) 3 "All even") (check-equal? (in-the-balance '(-1 -3 -5)) -3 "All odd (and negative!)") (check-equal? (in-the-balance '(2 3 4)) 1 "Mixed.") (check-equal? (sum-nums '(1 2 3 4 5)) 15 "List of numbers") (check-equal? (sum-nums '()) 0 "Empty check.") (check-equal? (avg-nums '(2 4 6)) 4 "List of numbers") (check-exn exn? (lambda () (avg-nums '())) "Should blow up.") (check-posn-equal? (avg-posn zpos) (make-posn 0 0) "Multiple zero points.") (check-posn-equal? (avg-posn lopos) (make-posn 3 3) "Valued points.") (check-exn exn? (lambda () (avg-posn '())) "Empty check. Divison by zero should occur.") (check-equal? (num-vowels "abc") 1 "One vowel") (check-equal? (num-vowels "bcd") 0 "No vowels") (check-equal? (num-vowels "") 0 "Empty string check") (check-equal? (num-vowels "aaaiieee!") 8 "Combination of AAAI and IEEE") (check-onion-equal? (los->onion str1) o1 "One-layer onion check.") (check-onion-equal? (los->onion str2) o2 "Bigger onion check.") (check-equal? (largest '(1 2 3 2 1)) 3 "In the middle") (check-equal? (largest '()) -inf.0 "Empty check.") (check-equal? (squares '(1 2 3)) '(1 4 9) "LON check") (check-equal? (squares '()) '() "Empty check") (check-equal? (largest* '((1 2 3) (3 8 2) (9 1 3))) '(3 8 9) "A lists with values") (check-equal? (largest* '((1 3 2) () (9 1 2))) '(3 -inf.0 9) "Including an empty list") (check-equal? (largest* '()) '() "Empty check.") (check-equal? (count-symbols '(a 3 5)) 1 "Easy check.") (check-equal? (count-symbols '()) 0 "Empty check.") (check-equal? (length* '((1 3 2) () (9 1 2))) 6 "Including an empty list") (check-equal? (length* '()) 0 "Empty check") ))) ;; Run all of the test suites. (define (capture-run) (parameterize ([current-output-port (open-output-string)]) (run-tests hof-tests) (get-output-string (current-output-port)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; THE GAME ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; If you're going to have a contest, you might as well ;; have a public scoreboard. ;; For testing at home. ;; (define aldenv48 (xmlrpc-server "localhost" 8080 "RPC2")) ;; Live (define aldenv48 (xmlrpc-server "141.195.226.48" 8080 "RPC2")) (define report (aldenv48 "report")) ;; I want to timeout this thread if it doesn't hit ;; the server in a timely manner. (let* ([run-msg (capture-run)] [try (thread (lambda () (with-handlers ([exn? (lambda (e) (printf run-msg))]) (printf (report (current-seconds) the-pair run-msg)))))]) (sleep 1) (when (thread-running? try) (kill-thread try) (printf run-msg)))