Skip to content
Snippets Groups Projects
Commit 15dcba34 authored by Yihao Zuo's avatar Yihao Zuo
Browse files

Upload New File

parent 6e104fdc
No related branches found
No related tags found
No related merge requests found
const fs = require('fs');
module.exports = {
// Exercise 1 - Iris Species Classifier
exercise1: (SepalLen, SepalWid, PetalLen, PetalWid) => {
if (PetalLen<2.5){
return 'setosa';}
else if (PetalWid<1.8){
if (PetalLen<5) {
if (PetalLen < 1.7) {
return 'versicolor';
}
else {
return 'virginica';
}
}
else if (PetalWid>=1.6) {
if (SepalLen < 7) {
return 'versicolor';
}
else {
return 'virginica';
}
}
else {
return 'virginica'}}
else if (PetalLen<4.9) {
if (SepalLen < 6) {
return 'versicolor';
}
else {
return 'virginica';
}
}
else {
return'virginica';}
},
// Exercise 2 - Dog Breeds Standards
exercise2: (breed, height, weight, male) => {
// Create an object that stores the standard height and standard weight of different types of dogs
var standards = {
"Balldog": {
"male": {
"height": 15,
"weight": 50
},
"female": {
"height": 14,
"weight": 40
}
},
"Dalmatian": {
"male": {
"height": 24,
"weight": 70
},
"female": {
"height": 19,
"weight": 45
}
},
"Maltese": {
"male": {
"height": 9,
"weight": 7
},
"female": {
"height": 7,
"weight": 6
}
}
}
// calculates whether the actual height and weight are between 90% and 110% of the standard values
if (breed in standards) {
var Puppy_height = standards[breed][male ? "male" : "female"].height;
var Puppy_weight = standards[breed][male ? "male" : "female"].weight;
if((0.9*Puppy_height > height) && (height > 1.1*Puppy_height)) {
if((0.9*Puppy_weight > weight) && (weight > 1.1*Puppy_weight)) {
return false;
}
return true;
}
return true;
} else {
return false;
}
},
// Exercise 3 - Basic Statistics
exercise3: (l) => {
l.sort();
/*creat var..*/
let min,max;
min = l[0];
max = l[l.length-1];
let sum = 0
for(let i = 0; i < l.length;i++) {
sum += l[i];
}
let aver,med;
aver = sum / l.length;
if(l.length % 2 === 0) {
med = (l[l.length/2] + l[l.length/2-1]) / 2;
}
else{
med = l[Math.floor(l.length/2)];
}
let List_1;
List_1 = [];
for(let i = 0; i < l.length;i++) {
List_1[i] = l[i]**2;
}
/*creat med^2,aver^2...*/
let aver2,med2,min2,max2,sum2;
min2 = List_1[0];
max2 = List_1[List_1.length-1];
sum2 = 0
for(let i = 0; i < List_1.length;i++) {
sum2 += List_1[i];
}
aver2 = sum2 / List_1.length;
if(List_1.length % 2 === 0) {
med2 = (List_1[List_1.length/2] + List_1[List_1.length/2-1]) / 2;
}
else {
med2 = List_1[Math.floor(List_1.length/2)];
}
return ([[min,aver,med,max],[min2,aver2,med2,max2]]);
},
// Exercise 4 - Finite-State Machine Simulator
exercise4: (trans, init_state, input_list) => {
let Li = [];
n = input_list.length;
for(let i = 0; i < n; i++) {
let key = init_state + '/'+ input_list[i];
if (key in trans) {
let n = trans[key];
init_state = n[0];
Li.push(n.slice(2));
}
}
return Li;
},
// Exercise 5 - Document Stats
exercise5: (filename) => {
return undefined;
},
// Exercise 6 - List Depth
exercise6: (l) => {
function maximum1(l) {
let a = []
for(let i = 0; i < l.length; i++) {
/*Check whether there is an array in the array*/
if (l[i] instanceof Array) {
a.push(maximum1(l[i]))
}
}
if(a.length > 0) {
/*Get the maximum value*/
return 1 + Math.max(...a)
}
return 1;
}
return maximum1(l)
},
// Exercise 7 - Change, please
exercise7: (amount,coins) => {
function f(amount, coins) {
var wallet = [2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01];
if (amount === 0 && coins === 0) {
return true;
} else {
for (var x of wallet) {
if (amount >= x) {
if ((amount - x === 0 && coins - 1 > 0) || (amount - x > 0 && coins - 1 === 0)) {
continue;
} else {
if (f(round(amount - x, 2), coins - 1)) {
return true;
}
}
}
}
return null;
}
}
if (f(amount, coins) === null) {
return false;
} else {
return true;
}
},
// Exercise 8 - Five Letter Unscramble
exercise8: (s) => {
return undefined;
},
// Exercise 9 - Wordle Set
exercise9: (green,yellow,gray) => {
return undefined;
},
// Exercise 10 - One Step of Wordle
exercise10: (green,yellow,gray) => {
return undefined;
},
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment