Skip to content

Commit 6407e80

Browse files
committed
lecture code for lecture 6 and 7
1 parent fee6510 commit 6407e80

12 files changed

+502
-0
lines changed

lec6_testing/Sort.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lec6_testing;
2+
3+
public class Sort {
4+
public static void sort(String[] x) {
5+
sort(x, 0);
6+
}
7+
8+
/** if we want recursion but we're not recursive yet
9+
* we can make a helper method, similar to get
10+
* in SLList
11+
*
12+
* This method sorts x starting from index s
13+
*/
14+
private static void sort(String[] x, int s) {
15+
if (s == x.length) {
16+
return;
17+
}
18+
// YOU SHOULD ONLY LOOK AT THE GREEN ITEMS
19+
int smallest = findSmallest(x, s);
20+
swap(x, s, smallest);
21+
sort(x, s + 1);
22+
}
23+
24+
// cat, bob, luka, c++
25+
// bob, cat, luka, c++
26+
// cat, bob, luka, c++
27+
28+
public static int findSmallest(String[] x, int s) {
29+
int smallestIndex = s;
30+
for (int i = s; i < x.length; i += 1) {
31+
int cmp = x[i].compareTo(x[smallestIndex]);
32+
if (cmp < 0) {
33+
smallestIndex = i;
34+
}
35+
}
36+
return smallestIndex;
37+
}
38+
39+
public static void swap(String[] x, int a, int b) {
40+
String temp = x[a];
41+
x[a] = x[b];
42+
x[b] = temp;
43+
}
44+
}

lec6_testing/TestSort.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package lec6_testing;
2+
import org.junit.jupiter.api.Test;
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
public class TestSort {
6+
@Test
7+
public void testSort() {
8+
String[] input = {"cat", "bob", "luka", "c++ is ..."};
9+
String[] expected = {"bob", "c++ is ...", "cat", "luka"};
10+
Sort.sort(input);
11+
12+
assertThat(input).isEqualTo(expected);
13+
}
14+
15+
@Test
16+
public void testFindSmallest() {
17+
String[] input = {"cat", "bob", "luka", "c++ is ..."};
18+
int expected = 1;
19+
20+
int actual = Sort.findSmallest(input, 0);
21+
assertThat(actual).isEqualTo(expected);
22+
23+
int expected2 = 3;
24+
int actual2 = Sort.findSmallest(input, 2);
25+
assertThat(actual2).isEqualTo(expected2);
26+
}
27+
28+
@Test
29+
public void testSwap() {
30+
String[] input = {"cat", "bob", "luka", "c++ is ..."};
31+
String[] expected = {"bob", "cat", "luka", "c++ is ..."};
32+
33+
Sort.swap(input, 0, 1);
34+
assertThat(input).isEqualTo(expected);
35+
36+
}
37+
}

lec7_lists4/AList.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lec7_lists4;
2+
3+
public class AList {
4+
private int size;
5+
private int[] items;
6+
7+
public AList() {
8+
size = 0;
9+
items = new int[100];
10+
}
11+
12+
// [3, 4, 2, 0, 0, 0, ....]
13+
// ^ (size = 3)
14+
// size is the location of the next add
15+
// size - 1 location of the last item
16+
17+
public void addLast(int x) {
18+
if (size == items.length) {
19+
resize(size + 1);
20+
}
21+
items[size] = x;
22+
size += 1;
23+
}
24+
25+
private void resize(int newSize) {
26+
int[] a = new int[newSize + 1];
27+
System.arraycopy(items, 0, a, 0, size);
28+
items = a;
29+
}
30+
31+
public int getLast() {
32+
return items[size - 1];
33+
}
34+
35+
public int get(int i) {
36+
return items[i];
37+
}
38+
39+
public int removeLast() {
40+
int itemToReturn = getLast();
41+
size -= 1;
42+
return itemToReturn;
43+
}
44+
}

lec7_lists4/AListTest.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package lec7_lists4;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static com.google.common.truth.Truth.assertThat;
6+
7+
/** This set of tests is not particularly exhaustive. For example,
8+
* it doesn't test that you can add items to the list, then delete
9+
* all of them, then add them back.
10+
*/
11+
public class AListTest {
12+
@Test
13+
public void testAddAndGet() {
14+
AList list = new AList();
15+
list.addLast(5);
16+
list.addLast(10);
17+
18+
// List should be [5, 10]
19+
assertThat(list.get(0)).isEqualTo(5);
20+
assertThat(list.get(1)).isEqualTo(10);
21+
}
22+
23+
@Test
24+
public void testGetLast() {
25+
AList list = new AList();
26+
list.addLast(7);
27+
list.addLast(14);
28+
list.addLast(21);
29+
30+
// List should be [7, 14, 21]
31+
assertThat(list.getLast()).isEqualTo(21);
32+
}
33+
34+
@Test
35+
public void testRemoveLast() {
36+
AList list = new AList();
37+
list.addLast(3);
38+
list.addLast(6);
39+
list.addLast(9);
40+
41+
// First remove should return 9
42+
assertThat(list.removeLast()).isEqualTo(9);
43+
44+
// And the list should be [3, 6] after that remove
45+
assertThat(list.get(0)).isEqualTo(3);
46+
assertThat(list.getLast()).isEqualTo(6);
47+
48+
// List should be [3] after second remove, with 6 returned
49+
assertThat(list.removeLast()).isEqualTo(6);
50+
assertThat(list.removeLast()).isEqualTo(3);
51+
}
52+
53+
/*@Test
54+
public void add200Items() {
55+
lec7_lists4.resizeExercise.AList list = new lec7_lists4.resizeExercise.AList();
56+
for (int i = 0; i < 200; i += 1) {
57+
list.addLast(i);
58+
}
59+
60+
for (int i = 0; i < 200; i += 1) {
61+
assertThat(list.get(i)).isEqualTo(i);
62+
}
63+
}*/
64+
}

lec7_lists4/resizeExercise/AList.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package lec7_lists4.resizeExercise;
2+
3+
public class AList {
4+
private int size;
5+
private int[] items;
6+
7+
public AList() {
8+
size = 0;
9+
items = new int[100];
10+
}
11+
12+
// [3, 4, 2, 0, 0, 0, ....]
13+
// ^ (size = 3)
14+
// size is the location of the next add
15+
// size - 1 location of the last item
16+
17+
public void addLast(int x) {
18+
items[size] = x;
19+
size += 1;
20+
}
21+
22+
public int getLast() {
23+
return items[size - 1];
24+
}
25+
26+
public int get(int i) {
27+
return items[i];
28+
}
29+
30+
public int removeLast() {
31+
int itemToReturn = getLast();
32+
size -= 1;
33+
return itemToReturn;
34+
}
35+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package lec7_lists4.resizeExercise;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static com.google.common.truth.Truth.assertThat;
6+
7+
/** This set of tests is not particularly exhaustive. For example,
8+
* it doesn't test that you can add items to the list, then delete
9+
* all of them, then add them back.
10+
*/
11+
public class AListTest {
12+
@Test
13+
public void testAddAndGet() {
14+
AList list = new AList();
15+
list.addLast(5);
16+
list.addLast(10);
17+
18+
// List should be [5, 10]
19+
assertThat(list.get(0)).isEqualTo(5);
20+
assertThat(list.get(1)).isEqualTo(10);
21+
}
22+
23+
@Test
24+
public void testGetLast() {
25+
AList list = new AList();
26+
list.addLast(7);
27+
list.addLast(14);
28+
list.addLast(21);
29+
30+
// List should be [7, 14, 21]
31+
assertThat(list.getLast()).isEqualTo(21);
32+
}
33+
34+
@Test
35+
public void testRemoveLast() {
36+
AList list = new AList();
37+
list.addLast(3);
38+
list.addLast(6);
39+
list.addLast(9);
40+
41+
// First remove should return 9
42+
assertThat(list.removeLast()).isEqualTo(9);
43+
44+
// And the list should be [3, 6] after that remove
45+
assertThat(list.get(0)).isEqualTo(3);
46+
assertThat(list.getLast()).isEqualTo(6);
47+
48+
// List should be [3] after second remove, with 6 returned
49+
assertThat(list.removeLast()).isEqualTo(6);
50+
assertThat(list.removeLast()).isEqualTo(3);
51+
}
52+
53+
@Test
54+
public void add200Items() {
55+
AList list = new AList();
56+
for (int i = 0; i < 200; i += 1) {
57+
list.addLast(i);
58+
}
59+
60+
for (int i = 0; i < 200; i += 1) {
61+
assertThat(list.get(i)).isEqualTo(i);
62+
}
63+
}
64+
}

lec7_lists4/speedtest/AList.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package lec7_lists4.speedtest;
2+
3+
public class AList {
4+
private int size;
5+
private int[] items;
6+
7+
public AList() {
8+
size = 0;
9+
items = new int[100];
10+
}
11+
12+
// [3, 4, 2, 0, 0, 0, ....]
13+
// ^ (size = 3)
14+
// size is the location of the next add
15+
// size - 1 location of the last item
16+
17+
public void addLast(int x) {
18+
if (size == items.length) {
19+
resize(size + 1);
20+
// other resizing strategies:
21+
// resize(size + 1000);
22+
// resize(size * 2));
23+
// resize((int) (size * 1.1));
24+
}
25+
items[size] = x;
26+
size += 1;
27+
}
28+
29+
private void resize(int newSize) {
30+
int[] a = new int[newSize];
31+
System.arraycopy(items, 0, a, 0, size);
32+
items = a;
33+
}
34+
35+
public int getLast() {
36+
return items[size - 1];
37+
}
38+
39+
public int get(int i) {
40+
return items[i];
41+
}
42+
43+
public int removeLast() {
44+
int itemToReturn = getLast();
45+
size -= 1;
46+
return itemToReturn;
47+
}
48+
}

lec7_lists4/speedtest/SLList.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lec7_lists4.speedtest;
2+
3+
/* List of anythings */
4+
public class SLList<Mustard> {
5+
private class MustardNode {
6+
public Mustard item; // first number in the list
7+
public MustardNode next; // rest of the list
8+
9+
public MustardNode(Mustard i, MustardNode n) {
10+
item = i;
11+
next = n;
12+
}
13+
}
14+
15+
16+
// the sentinel node is the dummy node at th front of the list
17+
// and the real first item is sentinel.next (if it exists)
18+
private MustardNode sentinel;
19+
private int size;
20+
21+
public SLList() {
22+
size = 0;
23+
sentinel = new MustardNode(null, null);
24+
}
25+
26+
public void addFirst(Mustard x) {
27+
size += 1;
28+
sentinel.next = new MustardNode(x, sentinel.next);
29+
}
30+
31+
public Mustard getFirst() {
32+
return sentinel.next.item;
33+
}
34+
35+
public void addLast(Mustard x) {
36+
size += 1;
37+
MustardNode p = sentinel;
38+
39+
// advance p until it is the last item
40+
while (p.next != null) {
41+
p = p.next;
42+
}
43+
44+
p.next = new MustardNode(x, null);
45+
}
46+
47+
public int size() {
48+
return size;
49+
}
50+
}

0 commit comments

Comments
 (0)