Compare commits

..

4 Commits

Author SHA1 Message Date
Isaac
4ecaa1a216 Add final test material 2022-12-20 21:49:28 -04:00
fcef1277f3 Merge remote-tracking branch 'origin/master' 2022-12-07 23:49:32 -04:00
8507082995 Assignment 6 skeleton 2022-12-07 23:49:22 -04:00
Isaac
e091a825ce Add quiz 3 content, and quiz 3 practice 2022-11-23 10:09:24 -04:00
15 changed files with 557 additions and 0 deletions

32
assignments/A6/classify.m Normal file
View File

@ -0,0 +1,32 @@
iris = csvread("iris.csv");
[training, testing] = randomsplit(iris, 2/3)
p = 2
cells = p^(columns(iris)-1)+1
minmax = ranges(iris);
classes = minmax(2,1) - minmax(1,1) + 1;
votes = zeros(cells,classes);
for i=1:rows(training)
label = training(i,1);
hashval = hash(training(i,:), minmax, p);
votes(hashval,label) += 1;
endfor
classification = tally(votes)
correct = 0
for i=1:rows(testing);
hashval = hash(testing(i,:), minmax, p);
class=classification(hashval);
label = testing(i,1);
if label == class
correct += 1;
endif
endfor
display(correct/rows(testing))

151
assignments/A6/iris.csv Normal file
View File

@ -0,0 +1,151 @@
1,5.1,3.5,1.4,0.2
1,4.9,3.0,1.4,0.2
1,4.7,3.2,1.3,0.2
1,4.6,3.1,1.5,0.2
1,5.0,3.6,1.4,0.2
1,5.4,3.9,1.7,0.4
1,4.6,3.4,1.4,0.3
1,5.0,3.4,1.5,0.2
1,4.4,2.9,1.4,0.2
1,4.9,3.1,1.5,0.1
1,5.4,3.7,1.5,0.2
1,4.8,3.4,1.6,0.2
1,4.8,3.0,1.4,0.1
1,4.3,3.0,1.1,0.1
1,5.8,4.0,1.2,0.2
1,5.7,4.4,1.5,0.4
1,5.4,3.9,1.3,0.4
1,5.1,3.5,1.4,0.3
1,5.7,3.8,1.7,0.3
1,5.1,3.8,1.5,0.3
1,5.4,3.4,1.7,0.2
1,5.1,3.7,1.5,0.4
1,4.6,3.6,1.0,0.2
1,5.1,3.3,1.7,0.5
1,4.8,3.4,1.9,0.2
1,5.0,3.0,1.6,0.2
1,5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 5.7 2.5
146 3 6.7 3.0 5.2 2.3
147 3 6.3 2.5 5.0 1.9
148 3 6.5 3.0 5.2 2.0
149 3 6.2 3.4 5.4 2.3
150 3 5.9 3.0 5.1 1.8

View File

@ -0,0 +1 @@
# Created by Octave 7.3.0, Wed Dec 07 23:43:44 2022 GMT <unknown@Isaac-DesktopPC>

View File

@ -0,0 +1,22 @@
#lang racket
(define (balance lst)
(define (helper lst counter)
(cond
[(empty? lst) counter] ;;base case
[(list? (first lst)) (helper (rest lst) counter)] ;;unwrap list
[(eq? (first lst) 'debit) (helper (rest lst) (- counter last))] ;;if debit subtract the amount
[(eq? (first lst) 'credit) (helper (rest lst) (+ counter last))] ;;if credit add the amount
[else (helper (rest lst) counter)]
)
)
(helper lst 0)
)
;; Racket
(module+ test
(require rackunit)
(check-equal? (balance (list (list 'credit 5))) 5)
(check-equal? (balance (list '(debit 5))) -5)
(check-equal? (balance '((debit 11) (credit 3))) -8)
(check-equal? (balance '((debit 3) (credit 5))) 2)
(check-equal? (balance '((debit 5) (credit 23) (debit 23) (credit 5))) 0))

View File

View File

@ -0,0 +1,19 @@
def invert(lst):
lst = list(lst) # Makes sure arugment is a list
#list_range = range(0, len(lst)) # Code I might have used if I used a dictionary comprehension
ret_dict = dict() # Create empty dict
counter = 0 # Create counter
for i in lst:
ret_dict[i] = counter # Assign each element of list to
# its postion in list
counter = counter + 1 # Increment counter
if (len(lst) > len(ret_dict)): # Check if the length of new dict is less than
return None # input list, if so, there is duplicates so return none
return ret_dict # Return created dictionary

View File

@ -0,0 +1,13 @@
from invert import invert
def test_empty():
assert invert([]) == {}
def test_simple():
invert(["three","two","one"]) == {"three": 0, "two":1, "one":2}
def test_duplicate():
assert invert(["bob","bob"]) == None
def test_numeric():
assert invert(range(0,6)) == { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5 }
def test_invert():
L=[-8,"pineapple",3]
D=invert(L)
assert [ L[D[j]] for j in L ] == L

View File

@ -0,0 +1,50 @@
class Expression {
constructor(op, left, right) {
this.op = op
this.left = left
this.right = right
}
eval() {
return evalExpression(this)
}
}
function evalExpression(expr) {
let tempLeft
let tempRight
if (typeof(expr.left) === "object") { // Check if left type is another expression
tempLeft = evalExpression(expr.left)
}
else {
tempLeft = expr.left
}
if (typeof(expr.right) === "object") { // Check if right type is another expression
tempRight = evalExpression(expr.right)
}
else {
tempRight = expr.right
}
if (typeof(tempLeft) === "number" & typeof(tempRight) === "number") { // Make sure both inputs are number
if (expr.op === "+") {
return tempLeft + tempRight
}
else if(expr.op === "-") {
return tempLeft - tempRight
}
else if(expr.op === "*") {
return tempLeft * tempRight
}
else if(expr.op === "/") {
return tempLeft / tempRight
}
else { // Case for when there is no valid provided operator
return "Invalid operator syntax"
}
}
else { //Case for when the left or right are not numbers
return "Invalid number syntax"
}
}
exports.Expression = Expression;

View File

@ -0,0 +1,71 @@
Expression = require("../expression.js").Expression;
let six_plus_nine = new Expression('+', 6, 9);
let six_times_nine = new Expression('*', 6, 9);
let six_minus_nine = new Expression('-', 6, 9);
let sixteen_div_eight = new Expression('/', 16, 8);
let compound1 = new Expression('+', six_times_nine, six_plus_nine)
let compound2 = new Expression('*', six_times_nine, compound1)
let compound3 = new Expression('+', compound2, 3)
describe("constructor",
function() {
let one = new Expression("+",0,1);
it("not null", () => expect(one).not.toBe(null));
it("op", () => expect(one.op).toBe("+"));
it("left", () => expect(one.left).toBe(0));
it("right", () => expect(one.right).toBe(1));
});
describe("simple",
function() {
it("+", () => expect(six_plus_nine.eval()).toBe(15));
it("-", () => expect(six_minus_nine.eval()).toBe(-3));
it("*", () => expect(six_times_nine.eval()).toBe(54));
it("/", () => expect(sixteen_div_eight.eval()).toBe(2));
});
describe("compound",
function() {
it("1", () => expect(compound1.eval()).toBe(69));
it("2", () => expect(compound2.eval()).toBe(3726));
it("3", () => expect(compound3.eval()).toBe(3729));
});
describe("floating point", //Testing floating point
function() {
let addPointFive = new Expression("+", 0.5, 0.5)
let timesPointFive = new Expression("*", 0.5, 10)
let minusPointFive = new Expression("-", 10, 0.5)
let dividePointFive = new Expression("/", 0.5, 0.1)
it("+", () => expect(addPointFive.eval()).toBe(1.0));
it("*", () => expect(timesPointFive.eval()).toBe(5.0));
it("-", () => expect(minusPointFive.eval()).toBe(9.5));
it("/", () => expect(dividePointFive.eval()).toBe(5.0));
});
describe("bad input", //Testing bad inputs
function() {
let invalidStringLeft = new Expression("+", "five", 6)
let invalidStringRight = new Expression("+", 5, "six")
let invalidOperator = new Expression("^", 6, 9)
it("Invalid String on left side", () => expect(invalidStringLeft.eval()).toBe("Invalid number syntax"));
it("Invalid String on right side", () => expect(invalidStringRight.eval()).toBe("Invalid number syntax"));
it("invalid operator", () => expect(invalidOperator.eval()).toBe("Invalid operator syntax"));
});
describe("compound bad input", //Testing bad inputs in compound cases
function() {
let invalidNumber = new Expression("+", "five", 6)
let invalidOperator = new Expression("^", 6, 9)
let semiValidCompound = new Expression("+", six_plus_nine, invalidNumber)
let completlyBadCompound = new Expression("+", invalidNumber, invalidOperator)
it("semi-valid", () => expect(semiValidCompound.eval()).toBe("Invalid number syntax"));
it("invalid", () => expect(completlyBadCompound.eval()).toBe("Invalid number syntax")); //Expected to be invalid number because it is the first error to be encountered
});

View File

@ -0,0 +1,13 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.?(m)js"
],
"helpers": [
"helpers/**/*.?(m)js"
],
"env": {
"stopSpecOnExpectationFailure": false,
"random": true
}
}

71
tests/Final/tests.txt Normal file
View File

@ -0,0 +1,71 @@
;; Racket
(module+ test
(require rackunit)
(check-equal? (balance (list (list 'credit 5))) 5)
(check-equal? (balance (list '(debit 5))) -5)
(check-equal? (balance '((debit 11) (credit 3))) -8)
(check-equal? (balance '((debit 3) (credit 5))) 2)
(check-equal? (balance '((debit 5) (credit 23) (debit 23) (credit 5))) 0))
# Python
from invert import invert
def test_empty():
assert invert([]) == {}
def test_simple():
invert(["three","two","one"]) == {"three": 0, "two":1, "one":2}
def test_duplicate():
assert invert(["bob","bob"]) == None
def test_numeric():
assert invert(range(0,6)) == { 0:0, 1:1, 2:2, 3:3, 4:4, 5:5 }
def test_invert():
L=[-8,"pineapple",3]
D=invert(L)
assert [ L[D[j]] for j in L ] == L
// JavaScript
Expression = require("../expression.js").Expression;
let six_plus_nine = new Expression('+', 6, 9);
let six_times_nine = new Expression('*', 6, 9);
let six_minus_nine = new Expression('-', 6, 9);
let sixteen_div_eight = new Expression('/', 16, 8);
let compound1 = new Expression('+', six_times_nine, six_plus_nine)
let compound2 = new Expression('*', six_times_nine, compound1)
let compound3 = new Expression('+', compound2, 3)
describe("constructor",
function() {
let one = new Expression("+",0,1);
it("not null", () => expect(one).not.toBe(null));
it("op", () => expect(one.op).toBe("+"));
it("left", () => expect(one.left).toBe(0));
it("right", () => expect(one.right).toBe(1));
});
describe("simple",
function() {
it("+", () => expect(six_plus_nine.eval()).toBe(15));
it("-", () => expect(six_minus_nine.eval()).toBe(-3));
it("*", () => expect(six_times_nine.eval()).toBe(54));
it("/", () => expect(sixteen_div_eight.eval()).toBe(2));
});
describe("compound",
function() {
it("1", () => expect(compound1.eval()).toBe(69));
it("2", () => expect(compound2.eval()).toBe(3726));
it("3", () => expect(compound3.eval()).toBe(3729));
});
# Octave
%!shared P1,P3
%! P1=[7,5,-3];
%! P3=[1,2,3;4,5,6;7,8,9];
%
%!assert(manypoly(P1,0),7,eps)
%!assert(manypoly(P1,1),9,eps)
%!assert(manypoly(P1,5),7+5*5-3*25,eps)
%!assert(manypoly(P3,0),[1;4;7],eps)
%!assert(manypoly(P3,1),[6;15;24],eps)
%!assert(manypoly(P3,2),[1+2*2+3*4;4+5*2+6*4;7+8*2+9*4],eps)

4
tests/Q3/prefix.py Normal file
View File

@ -0,0 +1,4 @@
def with_prefix(prefixes, words):
for prefix in prefixes:
lst = [word for word in words if word.startswith(prefix)]
yield lst

41
tests/Q3/test_prefix.py Normal file
View File

@ -0,0 +1,41 @@
from prefix import with_prefix
words=["apple","baby","abba"]
a_words=["apple", "abba"]
def test_simple():
assert list(with_prefix(["a"],words)) == [a_words]
def test_order():
assert list(with_prefix(["b","a"],words)) == [["baby"], a_words]
def test_multi():
assert list(with_prefix(["bb","ab"],words)) == [[],["abba"]]
# Commented out because the solution I am submitting is not using regex
#def test_regex1():
# assert list(with_prefix(["[a-z]b"], words)) == [ ["abba"] ]
#def test_regex2():
# assert list(with_prefix([".*a$"], words)) == [ ["abba"] ]
def test_gen():
gen = with_prefix(["b"],words)
assert next(gen) == ["baby"]
def test_gen2(): #Testing out of order prefixes, with generator syntax
gen = with_prefix(["b", "a"], words)
assert next(gen) == ["baby"]
assert next(gen) == ["apple", "abba"]
def test_gen3(): #Testing out returning the same number of elements as words, out of order
gen = with_prefix(["bab", "abb", "app"], words)
assert next(gen) == ["baby"]
assert next(gen) == ["abba"]
assert next(gen) == ["apple"]
words2 = ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "z"]
def test_gen4(): #Testing a long word and one letter word
gen = with_prefix(["a", "z"], words2)
assert next(gen) == ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]
assert next(gen) == ["z"]

View File

@ -0,0 +1,32 @@
def is_word(word):
counter = 0
for l in word:
if (counter % 2) == 0: #zero is vowel, one is constanant
if l == 'a' or l == 'e' or l == 'i' or l == 'o' or l == 'u':
counter = counter + 1
else:
return False
else:
if l == 'b' or l == 'k' or l == 'p' or l == 't' or l == 'z':
counter = counter + 1
else:
return False
return True
def cycle(lst):
while True:
yield lst
x = lst[0]
lst = lst[1:]
lst.append(x)
class Skippy:
def __init__(self, lst, offset)
self.lst = lst
self.offset = offset
self.counter = 0
def __next__(self)
if self.counter > length(self.lst)
self.counter = 0

View File

@ -0,0 +1,37 @@
from practice_questions import is_word
def test_match():
assert is_word("akataka") == True
assert is_word("ububu") == True
assert is_word("ikekezaza") == True
def test_extra():
assert is_word("akatakaa") == False
assert is_word("uububu") == False
def test_bad_letter():
assert is_word("yakataka") == False
assert is_word("akatakala") == False
def test_consonant_start():
assert is_word("kakataka") == False
assert is_word("bububu") == False
from practice_questions import cycle
def test_small():
lst = [1,2,3]
g = cycle(lst)
assert next(g) == lst
assert next(g) == [2,3,1]
assert next(g) == [3,1,2]
def test_big():
n = 5000
lst = list(range(n))
g = cycle(lst)
for j in range(n):
lst2 = next(g)
assert lst2[0] == n-1
lst3 = next(g)
assert lst3==lst