From 5be94f117c51fb7adc812d8140c69db6fe1fc3b7 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Mon, 8 Sep 2025 13:41:21 -0400 Subject: [PATCH] Added Welcome Back to Dr. Racket assignment. --- welcome-back-to-dr-racket/main.rkt | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 welcome-back-to-dr-racket/main.rkt diff --git a/welcome-back-to-dr-racket/main.rkt b/welcome-back-to-dr-racket/main.rkt new file mode 100644 index 0000000..737998c --- /dev/null +++ b/welcome-back-to-dr-racket/main.rkt @@ -0,0 +1,32 @@ +;; The first three lines of this file were inserted by DrRacket. They record metadata +;; about the language level of this file in a form that our tools can easily process. +#reader(lib "htdp-beginner-reader.ss" "lang")((modname main) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) +; List of Number -> String +; Determines whether a list of 3 numbers is ascending or descending. Throws error if neither. +(define (scending-how l) + (cond [(< (first l) (second l) (third l)) "ascending"] + [(> (first l) (second l) (third l)) "descending"] + [else (error "too complex")])) + +(check-expect (scending-how (list 1 2 3)) "ascending") +(check-expect (scending-how (list 5 4 3)) "descending") +(check-error (scending-how (list 1 0 3))) + + +; Number, Number -> Number +; Finds two positive numbers' greatest common denominator. +(define (gcd2 a b) + (cond + [(= a 0) b] + [(= b 0) a] + [(= 0 (+ (modulo a 2) (modulo b 2))) (* 2 (gcd2 (/ a 2) (/ b 2)))] + [(= 0 (modulo b 2)) (gcd2 a (/ b 2))] + [(= 0 (modulo a 2)) (gcd2 (/ a 2) b)] + [else (if (<= a b) (gcd2 a (- b a)) (gcd2 b (- a b)))])) + +(check-expect (gcd2 0 0) 0) +(check-expect (gcd2 100 0) 100) +(check-expect (gcd2 10 2) 2) +(check-expect (gcd2 10 10) 10) +(check-expect (gcd2 14 7) 7) +(check-expect (gcd2 288 64) 32)