Compare commits
6 Commits
d81425a781
...
master
Author | SHA1 | Date | |
---|---|---|---|
4ecaa1a216 | |||
fcef1277f3 | |||
8507082995 | |||
e091a825ce | |||
8501f2f473 | |||
bfdc5dc93b |
8
assignments/A5/.idea/.gitignore
generated
vendored
Normal file
8
assignments/A5/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
8
assignments/A5/.idea/A5.iml
generated
Normal file
8
assignments/A5/.idea/A5.iml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
6
assignments/A5/.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
assignments/A5/.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
4
assignments/A5/.idea/misc.xml
generated
Normal file
4
assignments/A5/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (python-venv)" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
8
assignments/A5/.idea/modules.xml
generated
Normal file
8
assignments/A5/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/A5.iml" filepath="$PROJECT_DIR$/.idea/A5.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
assignments/A5/.idea/vcs.xml
generated
Normal file
6
assignments/A5/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
1000
assignments/A5/2014-1000.csv
Normal file
1000
assignments/A5/2014-1000.csv
Normal file
File diff suppressed because one or more lines are too long
122
assignments/A5/readcsv.py
Normal file
122
assignments/A5/readcsv.py
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
def read_csv(filename):
|
||||||
|
"""Read a CSV file, return list of rows"""
|
||||||
|
|
||||||
|
import csv
|
||||||
|
with open(filename, 'rt', newline='') as f:
|
||||||
|
reader = csv.reader(f, skipinitialspace=True)
|
||||||
|
return [row for row in reader]
|
||||||
|
|
||||||
|
|
||||||
|
def header_map(headers):
|
||||||
|
"""
|
||||||
|
Read a list, and convert it to a dictionary where the key is each element of the given list and the value is
|
||||||
|
its position in the list
|
||||||
|
:param headers: List
|
||||||
|
:return: Dict
|
||||||
|
"""
|
||||||
|
|
||||||
|
header_dict = dict()
|
||||||
|
i = 0
|
||||||
|
for header in headers:
|
||||||
|
header_dict[header] = i
|
||||||
|
i = i + 1
|
||||||
|
return header_dict
|
||||||
|
|
||||||
|
|
||||||
|
def select(table, search_items):
|
||||||
|
"""
|
||||||
|
Read the set in the second argument and search through the table in the first argument and return the
|
||||||
|
columns that match the query in the search items
|
||||||
|
:param table: List
|
||||||
|
:param search_items: List
|
||||||
|
:return: List
|
||||||
|
"""
|
||||||
|
|
||||||
|
header_numbers = header_map(table[0])
|
||||||
|
ret_list = list()
|
||||||
|
columns = list()
|
||||||
|
|
||||||
|
for item in search_items:
|
||||||
|
if type(item) is int: # Convert searched elements into strings if it is a number
|
||||||
|
columns.append(header_numbers[str(item)])
|
||||||
|
else:
|
||||||
|
columns.append(header_numbers[item])
|
||||||
|
columns.sort()
|
||||||
|
for item in table:
|
||||||
|
lst = list()
|
||||||
|
for number in columns:
|
||||||
|
lst.append(item[number])
|
||||||
|
ret_list.append(lst)
|
||||||
|
return ret_list
|
||||||
|
|
||||||
|
|
||||||
|
def row2dict(hmap, row):
|
||||||
|
"""
|
||||||
|
Convert a row in the second argument, given the headers in the first argument to a dictionary which uses the
|
||||||
|
headers as keys and the row data as values
|
||||||
|
:param hmap: Dictionary
|
||||||
|
:param row: List
|
||||||
|
:return: Dictionary
|
||||||
|
"""
|
||||||
|
ret_dict = dict()
|
||||||
|
for key in hmap:
|
||||||
|
ret_dict[key] = row[hmap[key]]
|
||||||
|
return ret_dict
|
||||||
|
|
||||||
|
|
||||||
|
def check_row(row, query):
|
||||||
|
"""
|
||||||
|
Check the row in the first argument passes a query in the second argument. The second argument is a formatted
|
||||||
|
tuple where the first element in the tuple is a column name, the second is an operation (==, <=, >=, AND,
|
||||||
|
OR) and the third element is a condition, numeric or string matching. (ex: is age == 34, is color == blue). AND
|
||||||
|
and OR are special in that you can pass in recursive tuples (left and right argument are tuples) that will also
|
||||||
|
be evaluated recursively
|
||||||
|
:param row: List
|
||||||
|
:param query: Tuple
|
||||||
|
:return: Boolean
|
||||||
|
"""
|
||||||
|
|
||||||
|
def perform_operation(op, var, cond):
|
||||||
|
if type(var) is str:
|
||||||
|
if var.isnumeric():
|
||||||
|
var = int(var)
|
||||||
|
if type(cond) is str:
|
||||||
|
if cond.isnumeric():
|
||||||
|
cond = int(cond)
|
||||||
|
|
||||||
|
if op == '==':
|
||||||
|
return var == cond
|
||||||
|
elif op == '>=':
|
||||||
|
return var >= cond
|
||||||
|
elif op == '<=':
|
||||||
|
return var <= cond
|
||||||
|
elif op == 'OR':
|
||||||
|
return perform_operation(var[1], row[var[0]], var[2]) or perform_operation(cond[1], row[cond[0]], cond[2])
|
||||||
|
elif op == 'AND':
|
||||||
|
return perform_operation(var[1], row[var[0]], var[2]) and perform_operation(cond[1], row[cond[0]], cond[2])
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if type(query[0]) and type(query[2]) is tuple:
|
||||||
|
return perform_operation(query[1], query[0], query[2])
|
||||||
|
else:
|
||||||
|
stringify = str(query[2])
|
||||||
|
return perform_operation(query[1], row[str(query[0])], stringify)
|
||||||
|
|
||||||
|
|
||||||
|
def filter_table(table, query):
|
||||||
|
"""
|
||||||
|
This function takes a table of csv values, and performs the query to filter out the rows that do not match the query
|
||||||
|
:param table: List
|
||||||
|
:param query: Tuple
|
||||||
|
:return: List
|
||||||
|
"""
|
||||||
|
header_row = header_map(table[0])
|
||||||
|
data_rows = table[1:]
|
||||||
|
result = list()
|
||||||
|
result.append(table[0])
|
||||||
|
for row in data_rows:
|
||||||
|
data_dict = row2dict(header_row, row)
|
||||||
|
if check_row(data_dict, query):
|
||||||
|
result.append(row)
|
||||||
|
return result
|
4
assignments/A5/test1.csv
Normal file
4
assignments/A5/test1.csv
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
name, age, eye colour
|
||||||
|
Bob, 5, blue
|
||||||
|
Mary, 27, brown
|
||||||
|
Vij, 54, green
|
|
3
assignments/A5/test2.csv
Normal file
3
assignments/A5/test2.csv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
name, 100
|
||||||
|
teddy, 500
|
||||||
|
lovely, 1000
|
|
114
assignments/A5/test_readcsv.py
Normal file
114
assignments/A5/test_readcsv.py
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
from readcsv import read_csv
|
||||||
|
from readcsv import header_map
|
||||||
|
from readcsv import select
|
||||||
|
from readcsv import row2dict
|
||||||
|
from readcsv import check_row
|
||||||
|
from readcsv import filter_table
|
||||||
|
|
||||||
|
table = read_csv('test1.csv')
|
||||||
|
sampledata = read_csv('2014-1000.csv')
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_csv():
|
||||||
|
assert read_csv('test1.csv') == [['name', 'age', 'eye colour'],
|
||||||
|
['Bob', '5', 'blue'],
|
||||||
|
['Mary', '27', 'brown'],
|
||||||
|
['Vij', '54', 'green']]
|
||||||
|
|
||||||
|
|
||||||
|
def test_header_map_1():
|
||||||
|
hmap = header_map(table[0])
|
||||||
|
assert hmap == {'name': 0, 'age': 1, 'eye colour': 2}
|
||||||
|
|
||||||
|
|
||||||
|
def test_select_1():
|
||||||
|
assert select(table, {'name', 'eye colour'}) == [['name', 'eye colour'],
|
||||||
|
['Bob', 'blue'],
|
||||||
|
['Mary', 'brown'],
|
||||||
|
['Vij', 'green']]
|
||||||
|
|
||||||
|
|
||||||
|
def test_row2dict():
|
||||||
|
hmap = header_map(table[0])
|
||||||
|
assert row2dict(hmap, table[1]) == {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_row():
|
||||||
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
||||||
|
assert check_row(row, ('age', '==', 5))
|
||||||
|
assert not check_row(row, ('eye colour', '==', 5))
|
||||||
|
assert check_row(row, ('eye colour', '==', 'blue'))
|
||||||
|
assert check_row(row, ('age', '>=', 4))
|
||||||
|
assert check_row(row, ('age', '<=', 1000))
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_row_logical():
|
||||||
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
||||||
|
assert check_row(row, (('age', '==', 5), 'OR', ('eye colour', '==', 5)))
|
||||||
|
assert not check_row(row, (('age', '==', 5), 'AND', ('eye colour', '==', 5)))
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_table1():
|
||||||
|
assert filter_table(table, ('age', '>=', 0)) == [['name', 'age', 'eye colour'],
|
||||||
|
['Bob', '5', 'blue'],
|
||||||
|
['Mary', '27', 'brown'],
|
||||||
|
['Vij', '54', 'green']]
|
||||||
|
|
||||||
|
assert filter_table(table, ('age', '<=', 27)) == [['name', 'age', 'eye colour'],
|
||||||
|
['Bob', '5', 'blue'],
|
||||||
|
['Mary', '27', 'brown']]
|
||||||
|
|
||||||
|
assert filter_table(table, ('eye colour', '==', 'brown')) == [['name', 'age', 'eye colour'],
|
||||||
|
['Mary', '27', 'brown']]
|
||||||
|
|
||||||
|
assert filter_table(table, ('name', '==', 'Vij')) == [['name', 'age', 'eye colour'],
|
||||||
|
['Vij', '54', 'green']]
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_table2():
|
||||||
|
assert filter_table(table, (('age', '>=', 0), 'AND', ('age', '>=', '27'))) == [['name', 'age', 'eye colour'],
|
||||||
|
['Mary', '27', 'brown'],
|
||||||
|
['Vij', '54', 'green']]
|
||||||
|
|
||||||
|
assert filter_table(table, (('age', '<=', 27), 'AND', ('age', '>=', '27'))) == [['name', 'age', 'eye colour'],
|
||||||
|
['Mary', '27', 'brown']]
|
||||||
|
|
||||||
|
assert filter_table(table, (('eye colour', '==', 'brown'),
|
||||||
|
'OR',
|
||||||
|
('name', '==', 'Vij'))) == [['name', 'age', 'eye colour'],
|
||||||
|
['Mary', '27', 'brown'],
|
||||||
|
['Vij', '54', 'green']]
|
||||||
|
|
||||||
|
|
||||||
|
# Student Tests
|
||||||
|
table2 = read_csv('test2.csv')
|
||||||
|
hmap2 = header_map(table2[0])
|
||||||
|
|
||||||
|
|
||||||
|
def test_header_map2():
|
||||||
|
assert header_map(table2[0]) == {"name": 0, "100": 1}
|
||||||
|
|
||||||
|
|
||||||
|
def test_select2():
|
||||||
|
assert select(table2, [100]) == [["100"], ["500"], ["1000"]]
|
||||||
|
assert select(table2, ["name"]) == [["name"], ["teddy"], ["lovely"]]
|
||||||
|
assert select(table2, ["name", 100]) == [["name", "100"], ["teddy", "500"], ["lovely", "1000"]]
|
||||||
|
|
||||||
|
|
||||||
|
def test_row2dict2():
|
||||||
|
assert row2dict(hmap2, table2[1]) == {"name": "teddy", "100": "500"}
|
||||||
|
assert row2dict(hmap2, table2[2]) == {"name": "lovely", "100": "1000"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_row_2():
|
||||||
|
row = {'name': 'Bob', 'age': '5', 'eye colour': 'blue'}
|
||||||
|
assert not check_row(row, ('age', '===', 5))
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_row_3():
|
||||||
|
row = row2dict(hmap2, table2[1])
|
||||||
|
assert check_row(row, ("100", "==", 500))
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_table3():
|
||||||
|
assert filter_table(table2, ("100", ">=", 100)) == [["name", "100"], ["teddy", "500"], ["lovely", "1000"]]
|
1752
assignments/A5/test_readcsv_bigdata.py
Normal file
1752
assignments/A5/test_readcsv_bigdata.py
Normal file
File diff suppressed because it is too large
Load Diff
32
assignments/A6/classify.m
Normal file
32
assignments/A6/classify.m
Normal 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
151
assignments/A6/iris.csv
Normal 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.0,3.4,1.6,0.4
|
||||||
|
1,5.2,3.5,1.5,0.2
|
||||||
|
1,5.2,3.4,1.4,0.2
|
||||||
|
1,4.7,3.2,1.6,0.2
|
||||||
|
1,4.8,3.1,1.6,0.2
|
||||||
|
1,5.4,3.4,1.5,0.4
|
||||||
|
1,5.2,4.1,1.5,0.1
|
||||||
|
1,5.5,4.2,1.4,0.2
|
||||||
|
1,4.9,3.1,1.5,0.1
|
||||||
|
1,5.0,3.2,1.2,0.2
|
||||||
|
1,5.5,3.5,1.3,0.2
|
||||||
|
1,4.9,3.1,1.5,0.1
|
||||||
|
1,4.4,3.0,1.3,0.2
|
||||||
|
1,5.1,3.4,1.5,0.2
|
||||||
|
1,5.0,3.5,1.3,0.3
|
||||||
|
1,4.5,2.3,1.3,0.3
|
||||||
|
1,4.4,3.2,1.3,0.2
|
||||||
|
1,5.0,3.5,1.6,0.6
|
||||||
|
1,5.1,3.8,1.9,0.4
|
||||||
|
1,4.8,3.0,1.4,0.3
|
||||||
|
1,5.1,3.8,1.6,0.2
|
||||||
|
1,4.6,3.2,1.4,0.2
|
||||||
|
1,5.3,3.7,1.5,0.2
|
||||||
|
1,5.0,3.3,1.4,0.2
|
||||||
|
2,7.0,3.2,4.7,1.4
|
||||||
|
2,6.4,3.2,4.5,1.5
|
||||||
|
2,6.9,3.1,4.9,1.5
|
||||||
|
2,5.5,2.3,4.0,1.3
|
||||||
|
2,6.5,2.8,4.6,1.5
|
||||||
|
2,5.7,2.8,4.5,1.3
|
||||||
|
2,6.3,3.3,4.7,1.6
|
||||||
|
2,4.9,2.4,3.3,1.0
|
||||||
|
2,6.6,2.9,4.6,1.3
|
||||||
|
2,5.2,2.7,3.9,1.4
|
||||||
|
2,5.0,2.0,3.5,1.0
|
||||||
|
2,5.9,3.0,4.2,1.5
|
||||||
|
2,6.0,2.2,4.0,1.0
|
||||||
|
2,6.1,2.9,4.7,1.4
|
||||||
|
2,5.6,2.9,3.6,1.3
|
||||||
|
2,6.7,3.1,4.4,1.4
|
||||||
|
2,5.6,3.0,4.5,1.5
|
||||||
|
2,5.8,2.7,4.1,1.0
|
||||||
|
2,6.2,2.2,4.5,1.5
|
||||||
|
2,5.6,2.5,3.9,1.1
|
||||||
|
2,5.9,3.2,4.8,1.8
|
||||||
|
2,6.1,2.8,4.0,1.3
|
||||||
|
2,6.3,2.5,4.9,1.5
|
||||||
|
2,6.1,2.8,4.7,1.2
|
||||||
|
2,6.4,2.9,4.3,1.3
|
||||||
|
2,6.6,3.0,4.4,1.4
|
||||||
|
2,6.8,2.8,4.8,1.4
|
||||||
|
2,6.7,3.0,5.0,1.7
|
||||||
|
2,6.0,2.9,4.5,1.5
|
||||||
|
2,5.7,2.6,3.5,1.0
|
||||||
|
2,5.5,2.4,3.8,1.1
|
||||||
|
2,5.5,2.4,3.7,1.0
|
||||||
|
2,5.8,2.7,3.9,1.2
|
||||||
|
2,6.0,2.7,5.1,1.6
|
||||||
|
2,5.4,3.0,4.5,1.5
|
||||||
|
2,6.0,3.4,4.5,1.6
|
||||||
|
2,6.7,3.1,4.7,1.5
|
||||||
|
2,6.3,2.3,4.4,1.3
|
||||||
|
2,5.6,3.0,4.1,1.3
|
||||||
|
2,5.5,2.5,4.0,1.3
|
||||||
|
2,5.5,2.6,4.4,1.2
|
||||||
|
2,6.1,3.0,4.6,1.4
|
||||||
|
2,5.8,2.6,4.0,1.2
|
||||||
|
2,5.0,2.3,3.3,1.0
|
||||||
|
2,5.6,2.7,4.2,1.3
|
||||||
|
2,5.7,3.0,4.2,1.2
|
||||||
|
2,5.7,2.9,4.2,1.3
|
||||||
|
2,6.2,2.9,4.3,1.3
|
||||||
|
2,5.1,2.5,3.0,1.1
|
||||||
|
2,5.7,2.8,4.1,1.3
|
||||||
|
3,6.3,3.3,6.0,2.5
|
||||||
|
3,5.8,2.7,5.1,1.9
|
||||||
|
3,7.1,3.0,5.9,2.1
|
||||||
|
3,6.3,2.9,5.6,1.8
|
||||||
|
3,6.5,3.0,5.8,2.2
|
||||||
|
3,7.6,3.0,6.6,2.1
|
||||||
|
3,4.9,2.5,4.5,1.7
|
||||||
|
3,7.3,2.9,6.3,1.8
|
||||||
|
3,6.7,2.5,5.8,1.8
|
||||||
|
3,7.2,3.6,6.1,2.5
|
||||||
|
3,6.5,3.2,5.1,2.0
|
||||||
|
3,6.4,2.7,5.3,1.9
|
||||||
|
3,6.8,3.0,5.5,2.1
|
||||||
|
3,5.7,2.5,5.0,2.0
|
||||||
|
3,5.8,2.8,5.1,2.4
|
||||||
|
3,6.4,3.2,5.3,2.3
|
||||||
|
3,6.5,3.0,5.5,1.8
|
||||||
|
3,7.7,3.8,6.7,2.2
|
||||||
|
3,7.7,2.6,6.9,2.3
|
||||||
|
3,6.0,2.2,5.0,1.5
|
||||||
|
3,6.9,3.2,5.7,2.3
|
||||||
|
3,5.6,2.8,4.9,2.0
|
||||||
|
3,7.7,2.8,6.7,2.0
|
||||||
|
3,6.3,2.7,4.9,1.8
|
||||||
|
3,6.7,3.3,5.7,2.1
|
||||||
|
3,7.2,3.2,6.0,1.8
|
||||||
|
3,6.2,2.8,4.8,1.8
|
||||||
|
3,6.1,3.0,4.9,1.8
|
||||||
|
3,6.4,2.8,5.6,2.1
|
||||||
|
3,7.2,3.0,5.8,1.6
|
||||||
|
3,7.4,2.8,6.1,1.9
|
||||||
|
3,7.9,3.8,6.4,2.0
|
||||||
|
3,6.4,2.8,5.6,2.2
|
||||||
|
3,6.3,2.8,5.1,1.5
|
||||||
|
3,6.1,2.6,5.6,1.4
|
||||||
|
3,7.7,3.0,6.1,2.3
|
||||||
|
3,6.3,3.4,5.6,2.4
|
||||||
|
3,6.4,3.1,5.5,1.8
|
||||||
|
3,6.0,3.0,4.8,1.8
|
||||||
|
3,6.9,3.1,5.4,2.1
|
||||||
|
3,6.7,3.1,5.6,2.4
|
||||||
|
3,6.9,3.1,5.1,2.3
|
||||||
|
3,5.8,2.7,5.1,1.9
|
||||||
|
3,6.8,3.2,5.9,2.3
|
||||||
|
3,6.7,3.3,5.7,2.5
|
||||||
|
3,6.7,3.0,5.2,2.3
|
||||||
|
3,6.3,2.5,5.0,1.9
|
||||||
|
3,6.5,3.0,5.2,2.0
|
||||||
|
3,6.2,3.4,5.4,2.3
|
||||||
|
3,5.9,3.0,5.1,1.8
|
||||||
|
|
|
1
assignments/A6/workspace.octave-workspace
Normal file
1
assignments/A6/workspace.octave-workspace
Normal file
@ -0,0 +1 @@
|
|||||||
|
# Created by Octave 7.3.0, Wed Dec 07 23:43:44 2022 GMT <unknown@Isaac-DesktopPC>
|
22
tests/Final/Q1 Racket/balance.rkt
Normal file
22
tests/Final/Q1 Racket/balance.rkt
Normal 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))
|
0
tests/Final/Q1 Racket/balance.rkt~
Normal file
0
tests/Final/Q1 Racket/balance.rkt~
Normal file
19
tests/Final/Q2 Python/invert.py
Normal file
19
tests/Final/Q2 Python/invert.py
Normal 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
|
13
tests/Final/Q2 Python/test_invert.py
Normal file
13
tests/Final/Q2 Python/test_invert.py
Normal 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
|
50
tests/Final/Q3 Javascript/expression.js
Normal file
50
tests/Final/Q3 Javascript/expression.js
Normal 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;
|
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal file
71
tests/Final/Q3 Javascript/spec/expression.spec.js
Normal 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
|
||||||
|
});
|
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal file
13
tests/Final/Q3 Javascript/spec/support/jasmine.json
Normal 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
71
tests/Final/tests.txt
Normal 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
4
tests/Q3/prefix.py
Normal 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
41
tests/Q3/test_prefix.py
Normal 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"]
|
32
tests/practice/Q3/practice_questions.py
Normal file
32
tests/practice/Q3/practice_questions.py
Normal 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
|
||||||
|
|
37
tests/practice/Q3/test_practice_questions.py
Normal file
37
tests/practice/Q3/test_practice_questions.py
Normal 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
|
Reference in New Issue
Block a user