-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathruntime1-analyzing.js
95 lines (86 loc) · 2.13 KB
/
runtime1-analyzing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Read the following functions. For each one, figure out:
- What does the function do?
- Try to figure out the runtime -- O(1), O(log n), O(n), O(n log n), O(n^2), or O(2^n)
- Run the function with a few different input sizes and see how long it takes
- When the input size doubles, what happens to the time it takes to run?
*/
const mysteryFunction0 = function (array) {
const myFavoriteNum = 7;
for (i = 0; i < array.length; i++) {
if (array[i] === myFavoriteNum) {
return true;
}
}
return false;
};
const mysteryFunction1 = function (array) {
index = 4;
return array[index];
};
const mysteryFunction2 = function (n) {
const primes = [];
for (i = 2; i < n; i++) {
let factorFound = false;
for (j = 2; j < n; j++) {
if (i % j === 0 && i !== j) {
factorFound = true;
}
}
if (!factorFound) {
primes.push(i);
}
}
return primes.length;
};
const mysteryFunction3 = function (array) {
myLength = array.length;
if (myLength % 2 == 0) {
return "even length";
} else {
return "odd length";
}
};
const mysteryFunction4 = function (string) {
let eCount = 0;
for (i = 0; i < string.length; i++) {
if (string[i] === "e") {
eCount++;
}
}
return eCount;
};
const mysteryFunction5 = function (array) {
array.sort();
};
const mysteryFunction6 = function (dict, key) {
const value = dict[key];
return value;
};
const mysteryFunction7 = function (array) {
// Assume `array` is an array of ints sorted from smallest to biggest
const lookingFor = 9;
let lowerBound = 0;
let upperBound = array.length - 1;
let guessIndex = Math.floor(upperBound / 2);
while (lowerBound <= upperBound) {
if (array[guessIndex] === lookingFor) {
return true;
} else if (lookingFor < array[guessIndex]) {
upperBound = guessIndex - 1;
} else {
lowerBound = guessIndex + 1;
}
guessIndex = Math.floor((lowerBound + upperBound) / 2);
}
return false;
};
const mysteryFunction8 = function (dictionary) {
for (let key in dictionary) {
let value = dictionary[key];
if (key == value) {
return true;
}
return false;
}
};