-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextracode.txt
157 lines (144 loc) · 3.96 KB
/
extracode.txt
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const code = `
import React from "react";
import { render } from "react-dom";
//importing the test runner
import Test from "./Test";
//importing some code I want to test
import QuickSort from "./QuickSort";
import BubbleSort from "./BubbleSort";
const App = () => (
<div style={{ fontFamily: "sans-serif" }}>
<h3>✅ Running unit tests with Jest Test Compnent...</h3>
<div>
Simple Equality Tests...
<Test input={1} output={1} name="numbers" />
<Test input={"alice"} output={"alice"} name="strings" />
<Test input={[1, 2]} output={[1, 2]} name="arrays" />
<Test
input={{ name: "alice", loc: "paris" }}
output={{ name: "alice", loc: "paris" }}
name="objects"
/>
</div>
<br />
<div>
Testing Bubble Sort...
<Test input={BubbleSort([1])} output={[1]} name="empty array test" />
<Test
input={BubbleSort([1])}
output={[1]}
name="single element array test"
/>
<Test
input={BubbleSort([2, 1])}
output={[1, 2]}
name="two elements array test"
/>
<Test
input={BubbleSort([10, 5, 6, 2, 1])}
output={[1, 2, 5, 6, 10]}
name="many elements array test"
/>
</div>
<br />
<div>
Testing Quick Sort...
<Test input={QuickSort([])} output={[]} name="empty array test" />
<Test
input={QuickSort([1])}
output={[1]}
name="single element array test"
/>
<Test
input={QuickSort([2, 1])}
output={[1, 2]}
name="two elements array test"
/>
<Test
input={QuickSort([10, 5, 6, 2, 1])}
output={[1, 2, 5, 6, 10]}
name="many elements array test"
/>
</div>
</div>
);
render(<App />, document.getElementById("root"));
`;
const testCode = `
import React from "react";
import expect from "jest-matchers";
export default ({ input, output, name }) => {
let status = "✅";
try {
expect(input).toEqual(output);
} catch (AssertionError) {
console.log(AssertionError);
status = "❌";
}
return (
<div>
{status} {name}
</div>
);
};
`;
const quicksortCode = `
export default array => {
return quickSortRecursive(array, 0, array.length - 1);
};
const quickSortRecursive = (array, left, right) => {
let index;
if (array.length > 1) {
index = partition(array, left, right);
if (left < index - 1) {
quickSortRecursive(array, left, index - 1);
}
if (index < right) {
quickSortRecursive(array, index, right);
}
}
return array;
};
const partition = (array, left, right) => {
let mid = Math.floor((left + right) / 2);
let pivot = array[mid];
let il = left;
let ir = right;
while (il <= ir) {
//while the boundaries dont collide
while (array[il] < pivot) {
//while values on left are less than pivot, keep going
il++;
}
while (array[ir] > pivot) {
//while value on right are great than pivot, keep going
ir--;
}
if (il <= ir) {
//swap the values that are incorrectly placed
[array[il], array[ir]] = [array[ir], array[il]];
il++;
ir--;
}
}
return il; //return the correct position of the pivot in the array
};
`;
const bubblesortCode = `
export default array => {
let length = array.length;
for (let i = 0; i < length; i++) {
let swapped = false;
for (let j = 0; j < length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
swapped = true;
}
}
if (!swapped) {
break;
}
}
return array;
};
`;