Created
August 31, 2024 18:04
-
-
Save kmicinski/b38312a6edd21e67d55967f31811069e to your computer and use it in GitHub Desktop.
More practice for Project 1 in CIS352
This file contains 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
#lang racket | |
(provide (all-defined-out)) | |
;; | |
;; CIS352 (Fall '24) Racket Warmup | |
;; | |
(define (square x) (* x x)) | |
;; return the Euclidian distance between x0,y0 and x1,y1 | |
(define (euclid-distance x0 y0 x1 y1) | |
(sqrt (+ (expt (- y1 y0) 2) (expt (- x1 x0) 2)))) | |
;; return #t iff the list l contains a number less than i | |
(define (contains-less-than l i) | |
(match l | |
['() #f] | |
[`(,hd . ,tl) #:when (< hd i) #t] | |
[`(,hd . ,tl) (contains-less-than tl i)])) | |
;; valid "shapes" are (a) either rectangles with a lower-left x0/y0 | |
;; and upper-right x1/y1, (b) circles with some center x/y and radius | |
;; y. | |
(define (shape? s) | |
(match s | |
[`(circle ,x-enter ,y-center ,radius) #t] | |
[`(rect ,x0 ,y0 ,x1 ,y1) (and (< x0 x1) (< y0 y1))] | |
[_ #f])) | |
;; Calculate the area of a shape. | |
(define (shape-area s) | |
(match s | |
;; hint: use pi | |
[`(circle ,x-center ,y-center ,radius) (* pi radius radius)] | |
[`(rect ,x0 ,y0 ,x1 ,y1) (abs (* (- y1 y0) (- x1 x0)))])) | |
;; generate n spaces | |
(define (spaces n) (make-string n #\space)) | |
;; Assume `l` is a list of cons cells (pairs) whose first element is a | |
;; column position (natural number) and whose right element is a | |
;; character to be rendered at that line. You will return a string | |
;; representing the line. | |
;; | |
;; Example: | |
;; (draw-ascii-line '((3 . #\=) (4 . #\=) (5 . #\=) (7 . #\.) | |
;; (9 . #\=) (10 . #\=) (11 . #\=))) | |
;; > " === . ===" | |
(define (draw-ascii-line l) | |
(define (h l cur-pos) | |
(if (empty? l) | |
"" | |
;; else | |
(let ([next-index (car (first l))] | |
[next-char (cdr (first l))] | |
[rest-list (rest l)]) | |
(string-append (spaces (- next-index cur-pos)) | |
(make-string 1 next-char) | |
(h rest-list (add1 next-index)))))) | |
;; sort ascending, comparing only the column position by specifying | |
;; a comparison operator. | |
(h (sort l (lambda (x y) (< (car x) (car y)))) 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment