Skip to content

Commit 019159b

Browse files
committed
.
0 parents  commit 019159b

9 files changed

+312
-0
lines changed

ArrayExamples.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
public class ArrayExamples {
4+
5+
// Changes the input array to be in reversed order
6+
static void reverseInPlace(int[] arr) {
7+
for(int i = 0; i < arr.length; i += 1) {
8+
arr[i] = arr[arr.length - i - 1];
9+
}
10+
}
11+
12+
// Returns a *new* array with all the elements of the input array in reversed
13+
// order
14+
static int[] reversed(int[] arr) {
15+
int[] newArray = new int[arr.length];
16+
for(int i = 0; i < arr.length; i += 1) {
17+
arr[i] = newArray[arr.length - i - 1];
18+
}
19+
return arr;
20+
}
21+
22+
// Averages the numbers in the array (takes the mean), but leaves out the
23+
// lowest number when calculating. Returns 0 if there are no elements or just
24+
// 1 element in the array
25+
static double averageWithoutLowest(double[] arr) {
26+
if(arr.length < 2) { return 0.0; }
27+
double lowest = arr[0];
28+
for(double num: arr) {
29+
if(num < lowest) { lowest = num; }
30+
}
31+
double sum = 0;
32+
for(double num: arr) {
33+
if(num != lowest) { sum += num; }
34+
}
35+
return sum / (arr.length - 1);
36+
}
37+
38+
39+
}
40+

ArrayTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import static org.junit.Assert.*;
2+
import org.junit.*;
3+
4+
public class ArrayTests {
5+
@Test
6+
public void testReverseInPlace() {
7+
int[] input1 = { 3 };
8+
ArrayExamples.reverseInPlace(input1);
9+
assertArrayEquals(new int[]{ 3 }, input1);
10+
}
11+
12+
13+
@Test
14+
public void testReversed() {
15+
int[] input1 = { };
16+
assertArrayEquals(new int[]{ }, ArrayExamples.reversed(input1));
17+
}
18+
}

FileExample.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FileExample {
7+
8+
/*
9+
10+
Given a File (which can represent either a file or directory), return a list
11+
of all the files in that directory and all its subdirectories.
12+
13+
If given the path of a file (rather than a directory), it returns a list with
14+
just that element.
15+
16+
For example, for the following structure:
17+
18+
some-files/
19+
|- a.txt
20+
|- more-files/
21+
|- b.txt
22+
|- c.java
23+
|- even-more-files/
24+
|- d.java
25+
|- a.txt
26+
27+
Given new File("some-files/") as a parameter, we'd expect [some-files/a.txt,
28+
some-files/more-files/b.txt, some-files/more-files/c.java,
29+
some-files/even-more-files/d.java, some-files/even-more-files/a.txt] as results
30+
31+
Given new File("some-files/more-files") as a parameter, we'd expect
32+
[some-files/more-files/b.txt, some-files/more-files/c.java] as results
33+
34+
Given new File("some-files/a.txt") as a parameter, we'd expect
35+
[some-files/a.txt] and a result
36+
37+
See the File documentation here: https://docs.oracle.com/javase/8/docs/api/java/io/File.html
38+
39+
*/
40+
41+
static List<File> getFiles(File start) throws IOException {
42+
File f = start;
43+
List<File> result = new ArrayList<>();
44+
result.add(start);
45+
if(f.isDirectory()) {
46+
File[] paths = f.listFiles();
47+
for(File subFile: paths) {
48+
result.add(subFile);
49+
}
50+
}
51+
return result;
52+
}
53+
}
54+
55+

LectureExamples.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class MainExample1 {
2+
public static void main(String[] args) {
3+
int current = 0;
4+
while(current < args.length) {
5+
System.out.println(args[current]);
6+
}
7+
}
8+
}
9+
10+
class MainExample2 {
11+
public static void main(String[] args) {
12+
int length = args.length;
13+
for(int i = 0; i < length; length += 1) {
14+
System.out.println(args[i]);
15+
}
16+
}
17+
}
18+
19+
class EvensExample {
20+
static int sumEvenIndices(int[] nums) {
21+
int sum = 0;
22+
for(int i = 0; i < nums.length; i += 2) {
23+
sum += nums[i + 1];
24+
}
25+
return sum;
26+
}
27+
}
28+
29+
class NumsExample {
30+
static double reciprocal(int n) {
31+
return 1 / n;
32+
}
33+
static double ratio(int n, int d) {
34+
return n / d;
35+
}
36+
static double formula(int a, int b, int c) {
37+
return ratio(a, b) * reciprocal(c);
38+
}
39+
}

LinkedListExample.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import java.util.NoSuchElementException;
3+
4+
class Node {
5+
int value;
6+
Node next;
7+
public Node(int value, Node next) {
8+
this.value = value;
9+
this.next = next;
10+
}
11+
}
12+
class LinkedList {
13+
Node root;
14+
public LinkedList() {
15+
this.root = null;
16+
}
17+
/**
18+
* Adds the value to the _beginning_ of the list
19+
* @param value
20+
*/
21+
public void prepend(int value) {
22+
// Just add at the beginning
23+
this.root = new Node(value, this.root);
24+
}
25+
/**
26+
* Adds the value to the _end_ of the list
27+
* @param value
28+
*/
29+
public void append(int value) {
30+
if(this.root == null) {
31+
this.root = new Node(value, null);
32+
return;
33+
}
34+
// If it's just one element, add if after that one
35+
Node n = this.root;
36+
if(n.next == null) {
37+
n.next = new Node(value, null);
38+
return;
39+
}
40+
// Otherwise, loop until the end and add at the end with a null
41+
while(n.next != null) {
42+
n = n.next;
43+
n.next = new Node(value, null);
44+
}
45+
}
46+
/**
47+
* @return the value of the first element in the list
48+
*/
49+
public int first() {
50+
return this.root.value;
51+
}
52+
/**
53+
* @return the value of the last element in the list
54+
*/
55+
public int last() {
56+
Node n = this.root;
57+
// If no such element, throw an exception
58+
if(n == null) { throw new NoSuchElementException(); }
59+
// If it's just one element, return its value
60+
if(n.next == null) { return n.value; }
61+
// Otherwise, search for the end of the list and return the last value
62+
while(n.next != null) {
63+
n = n.next;
64+
}
65+
return n.value;
66+
}
67+
/**
68+
* @return a string representation of the list
69+
*/
70+
public String toString() {
71+
Node n = this.root;
72+
String s = "";
73+
while(n != null) {
74+
s += n.value + " ";
75+
n = n.next;
76+
}
77+
return s;
78+
}
79+
/**
80+
* @return the number of elements in the list
81+
*/
82+
public int length() {
83+
Node n = this.root;
84+
int i = 0;
85+
while(n != null) {
86+
i += 1;
87+
n = n.next;
88+
}
89+
return i;
90+
}
91+
}

ListExamples.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
interface StringChecker { boolean checkString(String s); }
5+
6+
class ListExamples {
7+
8+
// Returns a new list that has all the elements of the input list for which
9+
// the StringChecker returns true, and not the elements that return false, in
10+
// the same order they appeared in the input list;
11+
static List<String> filter(List<String> list, StringChecker sc) {
12+
List<String> result = new ArrayList<>();
13+
for(String s: list) {
14+
if(sc.checkString(s)) {
15+
result.add(0, s);
16+
}
17+
}
18+
return result;
19+
}
20+
21+
22+
// Takes two sorted list of strings (so "a" appears before "b" and so on),
23+
// and return a new list that has all the strings in both list in sorted order.
24+
static List<String> merge(List<String> list1, List<String> list2) {
25+
List<String> result = new ArrayList<>();
26+
int index1 = 0, index2 = 0;
27+
while(index1 < list1.size() && index2 < list2.size()) {
28+
if(list1.get(index1).compareTo(list2.get(index2)) < 0) {
29+
result.add(list1.get(index1));
30+
index1 += 1;
31+
}
32+
else {
33+
result.add(list2.get(index2));
34+
index2 += 1;
35+
}
36+
}
37+
while(index1 < list1.size()) {
38+
result.add(list1.get(index1));
39+
index1 += 1;
40+
}
41+
while(index2 < list2.size()) {
42+
result.add(list2.get(index2));
43+
index1 += 1;
44+
}
45+
return result;
46+
}
47+
48+
49+
}

MethodsTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import static org.junit.Assert.*;
2+
import org.junit.*;
3+
4+
public class MethodsTests {
5+
@Test
6+
public void testSumEvensLength4() {
7+
int[] input1 = { 12, 13, 7, 2};
8+
assertEquals(EvensExample.sumEvenIndices(input1), 19);
9+
}
10+
@Test
11+
public void testSumEvenLength5() {
12+
int[] input1 = { 12, 13, 7, 2, 33};
13+
assertEquals(EvensExample.sumEvenIndices(input1), 52);
14+
}
15+
@Test
16+
public void testSumEvenLength6() {
17+
int[] input1 = { 12, 13, 7, 8, 5, 3};
18+
assertEquals(EvensExample.sumEvenIndices(input1), 24);
19+
}
20+
}

lib/hamcrest-core-1.3.jar

44 KB
Binary file not shown.

lib/junit-4.13.2.jar

376 KB
Binary file not shown.

0 commit comments

Comments
 (0)