Last active
September 2, 2022 04:04
-
-
Save phondanai/9ee65537663e127bfe6644ce09d309c7 to your computer and use it in GitHub Desktop.
Solution of Lesson 0.4 Introduction to Racket from Northeastern University
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 2htdp/image) | |
; Ex 3: | |
; f->c : Real -> Real | |
; GIVEN: a temperature in degrees Fahrenheit as an argument | |
; RETURN: the equivalent temperature in degrees Celsius. | |
; Examples: | |
; (f->c 32) => 0 | |
; (f->c 100) => 37.7777777777.... | |
(define (f->c x) | |
(* (- x 32) | |
(/ 5 9))) | |
(check-expect (f->c 32) 0) | |
(check-within (f->c 100) 37.7 10) | |
; tip : Real Real[0.0,1.0] -> Number | |
; GIVEN: the amount of the bill in dollars and the | |
; percentage of tip | |
; RETURNS: the amount of the tip in dollars. | |
; Examples: | |
; (tip 10 0.15) => 1.5 | |
; (tip 20 0.17) => 3.4 | |
(define (tip x y) | |
(* x y)) | |
(check-expect (tip 10 0.15) 1.5) | |
(check-expect (tip 20 0.17) 3.4) | |
; sq : Number -> Number | |
; RETURN: square of a given number | |
; Examples: | |
; (sq 4) => 16 | |
; (sq 9) => 81 | |
(define (sq x) | |
(* x x)) | |
(check-expect (sq 4) 16) | |
(check-expect (sq 9) 81) | |
; Ex 6: | |
; quadratic-root : Real Real Real -> Real | |
; GIVEN: 3 arguments of the quadratic equation | |
; RETURN: root of the corresponding quadratic equation | |
; Examples: | |
; (quadratic-root 1 0 -4) => 0 | |
; (quadratic-root 1 -2 1) => 0 | |
(define (quadratic-root a b c) | |
(sqrt (/ (+ (- b) | |
(sqrt (- (* b b) (* 4 a c)))) | |
(* 2 a)))) | |
(check-within (quadratic-root 1 0 -4) 0 2) | |
(check-within (quadratic-root 1 -2 1) 0 2) | |
; Ex 7: | |
; circumference : Real -> Real | |
; GIVEN: the radius of a circle | |
; RETURNS: its circumference, using the fomula 2 * pi * r | |
; Examples: | |
; (circumference 1) => 2 * pi | |
; (circumference 0) => 0 | |
(define (circumference r) | |
(* 2 pi r)) | |
(check-within (circumference 1) (* 2 pi) 4) | |
(check-expect (circumference 0) 0) | |
; Ex 8: | |
; circle-area : Real -> Real | |
; RETURNS: area of circle using given radius | |
; Examples: | |
; (circle-area 1) => pi | |
; (circle-area 5) => 78.53 | |
; (circle-area 7) => 153.93 | |
(define (circle-area r) | |
(* pi r r)) | |
(check-within (circle-area 1) pi 2) | |
(check-within (circle-area 5) 78.53 2) | |
(check-within (circle-area 7) 153.93 2) | |
; Ex 9: | |
; is-even? : Number -> Boolean | |
; RETURN: produce true if given number is even number otherwise false | |
; Examples: | |
; (is-even? 2) => true | |
; (is-even? 1) => false | |
; (is-even? 0) => true | |
(define (is-even? x) | |
(= (remainder x 2) 0)) | |
(check-expect (is-even? 2) true) | |
(check-expect (is-even? 1) false) | |
(check-expect (is-even? 0) true) | |
; Ex 10: | |
; sum-of-two-larger : Number Number Number -> Number | |
; RETURN: sum of the two larger numbers | |
; Examples: | |
; (sum-of-two-larger 10 20 30) => 50 | |
; (sum-of-two-larger 5 1 3) => 8 | |
(define (sum-of-two-larger x y z) | |
(cond | |
[(and (< x y) (< y z)) (+ y z)] | |
[(and (< y x) (< x z)) (+ x z)] | |
[(and (< z x) (< x y)) (+ x y)] | |
[(and (< y x) (< y z)) (+ x z)] | |
[(and (< z x) (< z y)) (+ x y)] | |
[(and (< x y) (< z y)) (+ y z)])) | |
(check-expect (sum-of-two-larger 10 20 30) 50) | |
(check-expect (sum-of-two-larger 5 1 3) 8) | |
(check-expect (sum-of-two-larger 5 1 6) 11) | |
(check-expect (sum-of-two-larger 5 10 4) 15) | |
(check-expect (sum-of-two-larger 7 6 5) 13) | |
(check-expect (sum-of-two-larger 5 10 9) 19) | |
(define-struct point (x y)) | |
;; A Point is a (make-point Number Number) | |
;; It represents a position on the screen | |
;; Interpretation: | |
;; x = the x-coordinate on the screen (in pixel from the left). | |
;; y = the y-coordinate on the screen (in pixel from the top). | |
; Ex 15: | |
(define-struct student (id name major)) | |
;; A Student is a (make-student Number String String) | |
;; It represents a student data | |
;; Interpretation: | |
;; id = the student id | |
;; name = the student name | |
;; major = the student major of study | |
(rectangle 2 4 "solid" "blue") | |
(rectangle 4 8 "solid" "blue") | |
(rectangle 8 16 "solid" "blue") | |
(rectangle 16 32 "solid" "blue") | |
; Ex 18: | |
; rec-sequence : Number -> Rectangle | |
; RETURN: return the nth rectangle in the sequence formula | |
; Examples: | |
; (rec-sequence 2) => (rectangle 4 8 "solid" "blue") | |
; (rec-sequence 4) => (rectangle 16 32 "solid" "blue") | |
(define (rec-sequence n) | |
(rectangle (expt 2 n) | |
(expt 2 (+ n 1)) | |
"solid" | |
"blue")) | |
(check-expect (rec-sequence 2) (rectangle 4 8 "solid" "blue")) | |
(check-expect (rec-sequence 4) (rectangle 16 32 "solid" "blue")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment