Skip to content

Commit

Permalink
assignment 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbui904 committed Jan 12, 2022
1 parent 18258a4 commit aa30d68
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 0 deletions.
3 changes: 3 additions & 0 deletions assignment2-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# assignment2-starter

Starter code for Assignment 2.
1 change: 1 addition & 0 deletions assignment2-starter/collaborations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I did not consulted with anyone outside of class for this particular assignment.
8 changes: 8 additions & 0 deletions assignment2-starter/factorial.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(define fact
(lambda (n)
(if (equal? n 1)
1
(* n (fact (- n 1))))))

,tr (fact 5)

33 changes: 33 additions & 0 deletions assignment2-starter/main.scm
Original file line number Diff line number Diff line change
@@ -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)))))))








2 changes: 2 additions & 0 deletions assignment2-starter/scheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export GUILE_AUTO_COMPILE=0
rlwrap guile "$@"
1 change: 1 addition & 0 deletions assignment2-starter/test-m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./scheme tester-script.scm "tests-m.scm"
39 changes: 39 additions & 0 deletions assignment2-starter/tester-script.scm
Original file line number Diff line number Diff line change
@@ -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)))
30 changes: 30 additions & 0 deletions assignment2-starter/tests-m.scm
Original file line number Diff line number Diff line change
@@ -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)))
77 changes: 77 additions & 0 deletions assignment3-starter/main.scm
Original file line number Diff line number Diff line change
@@ -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 () ())))
2 changes: 2 additions & 0 deletions assignment3-starter/scheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export GUILE_AUTO_COMPILE=0
rlwrap guile "$@"
1 change: 1 addition & 0 deletions assignment3-starter/test-m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./scheme tester-script.scm "tests-m.scm"
39 changes: 39 additions & 0 deletions assignment3-starter/tester-script.scm
Original file line number Diff line number Diff line change
@@ -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)))
84 changes: 84 additions & 0 deletions assignment3-starter/tests-m.scm
Original file line number Diff line number Diff line change
@@ -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 '()))

0 comments on commit aa30d68

Please sign in to comment.