diff --git a/assignment2-starter/README.md b/assignment2-starter/README.md new file mode 100644 index 0000000..ccb0a0c --- /dev/null +++ b/assignment2-starter/README.md @@ -0,0 +1,3 @@ +# assignment2-starter + +Starter code for Assignment 2. \ No newline at end of file diff --git a/assignment2-starter/collaborations.txt b/assignment2-starter/collaborations.txt new file mode 100644 index 0000000..b5650cd --- /dev/null +++ b/assignment2-starter/collaborations.txt @@ -0,0 +1 @@ +I did not consulted with anyone outside of class for this particular assignment. \ No newline at end of file diff --git a/assignment2-starter/factorial.scm b/assignment2-starter/factorial.scm new file mode 100644 index 0000000..89585af --- /dev/null +++ b/assignment2-starter/factorial.scm @@ -0,0 +1,8 @@ +(define fact + (lambda (n) + (if (equal? n 1) + 1 + (* n (fact (- n 1)))))) + +,tr (fact 5) + diff --git a/assignment2-starter/main.scm b/assignment2-starter/main.scm new file mode 100755 index 0000000..9e50809 --- /dev/null +++ b/assignment2-starter/main.scm @@ -0,0 +1,33 @@ +; Written by Thien K. M Bui +; Last updated 01/09/2022 +; HW02 CS251 Winter 2022 + +; sum +; param: list of integers +; return: sum of all integers in list + +(define sum + (lambda (currentList) + (if (equal? (length currentList) 0) + 0 + (+ (car currentList) (sum (cdr currentList)))))) + +; keep-first-n +; param: list of items, integer n +; return: items in list up to n provided index, +; if n is 0, bigger than length of list, or negative, return empty list + +(define keep-first-n + (lambda (index currentList) + (cond + ((<= index 0) '()) + ((> index (length currentList)) '()) + (else (cons (car currentList) (keep-first-n (- index 1) (cdr currentList))))))) + + + + + + + + diff --git a/assignment2-starter/scheme b/assignment2-starter/scheme new file mode 100755 index 0000000..69e928f --- /dev/null +++ b/assignment2-starter/scheme @@ -0,0 +1,2 @@ +export GUILE_AUTO_COMPILE=0 +rlwrap guile "$@" diff --git a/assignment2-starter/test-m b/assignment2-starter/test-m new file mode 100755 index 0000000..6061803 --- /dev/null +++ b/assignment2-starter/test-m @@ -0,0 +1 @@ +./scheme tester-script.scm "tests-m.scm" diff --git a/assignment2-starter/tester-script.scm b/assignment2-starter/tester-script.scm new file mode 100644 index 0000000..46c9162 --- /dev/null +++ b/assignment2-starter/tester-script.scm @@ -0,0 +1,39 @@ +;; Based on https://srfi.schemers.org/srfi-64/srfi-64.html Unit testing set up +;; code below. Ignore most of this; the important part to focus on is the tests +;; in the associated files. +(use-modules (ice-9 format)) +(use-modules (srfi srfi-64)) +(define (my-simple-runner) + (let ((runner (test-runner-null)) + (num-passed 0) + (num-failed 0)) + (test-runner-on-test-end! runner + (lambda (runner) + (case (test-result-kind runner) + ((pass xpass) (set! num-passed (+ num-passed 1))) + ((fail xfail) + (format #t "FAILED:\n ~s:line ~s\n ~s\n" + (test-result-ref runner 'source-file) + (test-result-ref runner 'source-line) + (test-result-ref runner 'source-form)) + + (set! num-failed (+ num-failed 1))) + (else #t)))) + (test-runner-on-final! runner + (lambda (runner) + (format #t "Passing tests: ~d.~%Failing tests: ~d.~%" + num-passed num-failed))) + runner)) + +(load "main.scm") + +(test-runner-factory + (lambda () (my-simple-runner))) + +(define test-file-name (cadr (command-line))) + +(test-begin "test suite") +(load test-file-name) +(test-end "m test suite") + +(exit (test-runner-fail-count (test-runner-current))) diff --git a/assignment2-starter/tests-m.scm b/assignment2-starter/tests-m.scm new file mode 100755 index 0000000..b589b21 --- /dev/null +++ b/assignment2-starter/tests-m.scm @@ -0,0 +1,30 @@ +;; Do not change these tests! We will be using the +;; original files to grade, so changing the tests will +;; only make your own preliminary testing inaccurate. + +;; Module to include testing code +(use-modules (srfi srfi-64)) + +;; Basic tests for sum +(test-equal 10 (sum '(4 5 0 1))) +(test-equal 5 (sum '(4 5 0 1 -2 -3))) +(test-equal 3 (sum '(3))) + +;; Basic tests for keep-first-n +(test-equal '(a b c) (keep-first-n 3 '(a b c d e))) +(test-equal '(a b) (keep-first-n 2 '(a b c d e))) +(test-equal '(a b c d e) (keep-first-n 5 '(a b c d e))) +(test-equal '(a) (keep-first-n 1 '(a b c d e))) +(test-equal '(d) (keep-first-n 1 '(d))) + +;; Advanced test for sum (assignment specifies that 0 +;; should be returned for the empty list) +(test-equal 0 (sum '())) + + +;; Advanced tests for keep-first-n (assignment specifies +;; that the empty list should be returned when n is +;; negative or larger than the length of the input list) +(test-equal '() (keep-first-n 6 '(a b c d e))) +(test-equal '() (keep-first-n -1 '(a b c d e))) +(test-equal '() (keep-first-n 0 '(a b c))) diff --git a/assignment3-starter/main.scm b/assignment3-starter/main.scm new file mode 100644 index 0000000..9a6d0b5 --- /dev/null +++ b/assignment3-starter/main.scm @@ -0,0 +1,77 @@ +;Written by Thien K. M. Bui +; Last modified 01/11/2022 +; HW03 CS251 Winter 2022, bst assignment + +; entry +; param bst +; return the root node of provided bst +(define entry + (lambda (bst) + (cond + ;if not a list, return #f + ((not (list? bst)) #f) + ((not (= (length bst) 3)) #f) + ;check the 2nd element + ((not (list? (car (cdr bst)))) #f) + ;check the 3rd element of a list + ((not (list? (car (cdr (cdr bst))))) #f) + (else (car bst))))) + +;left +;param bst +; return left subtree of provided bst +(define left + (lambda (bst) + (cond + ((not (list? bst)) #f) + ((not (= (length bst) 3)) #f) + ;check the 2nd element + ((not (list? (car (cdr bst)))) #f) + ;check the 3rd element of a list + ((not (list? (car (cdr (cdr bst))))) #f) + (else (car (cdr bst)))))) + +;right +;param bst +;return the right subtree of provided bst +(define right + (lambda (bst) + (cond + ((not (list? bst)) #f) + ((not (= (length bst) 3)) #f) + ;check the 2nd element + ((not (list? (car (cdr bst)))) #f) + ;check the 3rd element of a list + ((not (list? (car (cdr (cdr bst))))) #f) + (else (car (cdr (cdr bst))))))) + +;make-bst +; param: root node, left-bst, right-bst +; return new tree whose node is elt, left subtree is left-bst, right subtree is right-bst +(define make-bst + (lambda (elt left-bst right-bst) + (cond + ;if elt is bad + ( (not (integer? elt)) #f) + ;if left-bst is bad (not of length 3 and 0 or not a list) + ( (or (and (not (= (length left-bst) 0)) (not (= (length left-bst) 3))) (not (list? left-bst))) #f) + ;if right-bst is bad + ( (or (and (not (= (length right-bst) 0)) (not (= (length right-bst) 3))) (not (list? right-bst))) #f) + (else (list elt left-bst right-bst))))) + +;pre-order +; param: bst +; return all nodes within bst in pre-order +(define preorder + (lambda (bst) + ;return bst + ;make list of pre-order(left-subtree) + ;make list of pre-order(right-subtree) + + ;use cons + (if (null? bst) + '() + (append (entry bst) (preorder (left bst)) (preorder (right bst)))))) + + +; (preorder '(1 (2 () ()) (3 () ()))) \ No newline at end of file diff --git a/assignment3-starter/scheme b/assignment3-starter/scheme new file mode 100755 index 0000000..22d0024 --- /dev/null +++ b/assignment3-starter/scheme @@ -0,0 +1,2 @@ +export GUILE_AUTO_COMPILE=0 +rlwrap guile "$@" \ No newline at end of file diff --git a/assignment3-starter/test-m b/assignment3-starter/test-m new file mode 100755 index 0000000..64ceacb --- /dev/null +++ b/assignment3-starter/test-m @@ -0,0 +1 @@ +./scheme tester-script.scm "tests-m.scm" \ No newline at end of file diff --git a/assignment3-starter/tester-script.scm b/assignment3-starter/tester-script.scm new file mode 100644 index 0000000..46c9162 --- /dev/null +++ b/assignment3-starter/tester-script.scm @@ -0,0 +1,39 @@ +;; Based on https://srfi.schemers.org/srfi-64/srfi-64.html Unit testing set up +;; code below. Ignore most of this; the important part to focus on is the tests +;; in the associated files. +(use-modules (ice-9 format)) +(use-modules (srfi srfi-64)) +(define (my-simple-runner) + (let ((runner (test-runner-null)) + (num-passed 0) + (num-failed 0)) + (test-runner-on-test-end! runner + (lambda (runner) + (case (test-result-kind runner) + ((pass xpass) (set! num-passed (+ num-passed 1))) + ((fail xfail) + (format #t "FAILED:\n ~s:line ~s\n ~s\n" + (test-result-ref runner 'source-file) + (test-result-ref runner 'source-line) + (test-result-ref runner 'source-form)) + + (set! num-failed (+ num-failed 1))) + (else #t)))) + (test-runner-on-final! runner + (lambda (runner) + (format #t "Passing tests: ~d.~%Failing tests: ~d.~%" + num-passed num-failed))) + runner)) + +(load "main.scm") + +(test-runner-factory + (lambda () (my-simple-runner))) + +(define test-file-name (cadr (command-line))) + +(test-begin "test suite") +(load test-file-name) +(test-end "m test suite") + +(exit (test-runner-fail-count (test-runner-current))) diff --git a/assignment3-starter/tests-m.scm b/assignment3-starter/tests-m.scm new file mode 100644 index 0000000..7dd1087 --- /dev/null +++ b/assignment3-starter/tests-m.scm @@ -0,0 +1,84 @@ +;; DO NOT CHANGE THESE TESTS. + +;; Module to include testing code +(use-modules (srfi srfi-64)) + +;; Basic tests for entry, left, right +(test-equal 1 + (entry '(1 () ()))) +(test-equal '(2 () ()) + (left '(1 (2 () ()) (3 () ())))) +(test-equal '(3 () ()) + (right '(1 (2 () ()) (3 () ())))) + +;; Basic tests for make-bst +(test-equal '(3 () ()) + (make-bst 3 '() '())) + +(test-equal '(3 (1 () (2 () ())) (6 (4 () ()) (18 () ()))) + (make-bst 3 '(1 () (2 () ())) '(6 (4 () ()) (18 () ())))) + +;; Basci test for orderings +(test-equal '(1 2 3) + (preorder '(1 (2 () ()) (3 () ())))) + +(let ((test-tree '(8 (4 () (6 () (7 () ()))) + (28 (22 () (23 () ())) ())))) + (test-equal '(8 4 6 7 28 22 23) + (preorder test-tree)) + ) + + (test-equal '(2 1 3) + (inorder '(1 (2 () ()) (3 () ())))) + + (test-equal '(2 3 1) + (postorder '(1 (2 () ()) (3 () ())))) + +;; Basic tests for insert +(test-equal '(5 () ()) + (insert 5 '()) ) +(test-equal '(6 (5 () ()) ()) + (insert 5 '(6 () ()))) +(test-equal '(4 () (5 () ())) + (insert 5 '(4 () ()))) +(test-equal '(8 (4 () (6 () (7 () ()))) (28 (22 (10 () ()) (23 () ())) ())) + (insert 10 '(8 (4 () (6 () (7 () ()))) (28 (22 () (23 () ())) ())))) + + ;; Advanced tests for orderings +(let ((test-tree '(8 (4 () (6 () (7 () ()))) + (28 (22 () (23 () ())) ())))) + (test-equal '(4 6 7 8 22 23 28) + (inorder test-tree)) + (test-equal '(7 6 4 23 22 28 8) + (postorder test-tree) ) + ) + + ;; Advanced tests for entry, left, right (edge cases) + (test-assert (not (entry '(a b c)))) + (test-assert (not (left '(a b c)))) + (test-assert (not (right '(a b c)))) + (test-assert (not (entry '()))) + (test-assert (not (left '()))) + (test-assert (not (right '()))) + + ;; Advanced tests for make-bst (edge cases) + (test-assert (not (make-bst 1 2 3))) + (test-assert (not (make-bst 1.5 '() '()))) + (test-assert (not (make-bst 'a '() '()))) + (test-assert (not (make-bst 1 '(2) '()))) + (test-assert (not (make-bst 1 '() '(3)))) + (test-assert (not (make-bst 1 '(5 6 7) '(2 3 4)))) + +;; Advanced tests for orderings (edge cases) + (test-equal '(1) + (preorder '(1 () ()))) + (test-equal '(1) + (inorder '(1 () ()))) + (test-equal '(1) + (postorder '(1 () ()))) + (test-equal '() + (preorder '())) + (test-equal '() + (inorder '())) + (test-equal '() + (postorder '()))