Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpolitz committed Oct 12, 2022
0 parents commit 019159b
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ArrayExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


public class ArrayExamples {

// Changes the input array to be in reversed order
static void reverseInPlace(int[] arr) {
for(int i = 0; i < arr.length; i += 1) {
arr[i] = arr[arr.length - i - 1];
}
}

// Returns a *new* array with all the elements of the input array in reversed
// order
static int[] reversed(int[] arr) {
int[] newArray = new int[arr.length];
for(int i = 0; i < arr.length; i += 1) {
arr[i] = newArray[arr.length - i - 1];
}
return arr;
}

// Averages the numbers in the array (takes the mean), but leaves out the
// lowest number when calculating. Returns 0 if there are no elements or just
// 1 element in the array
static double averageWithoutLowest(double[] arr) {
if(arr.length < 2) { return 0.0; }
double lowest = arr[0];
for(double num: arr) {
if(num < lowest) { lowest = num; }
}
double sum = 0;
for(double num: arr) {
if(num != lowest) { sum += num; }
}
return sum / (arr.length - 1);
}


}

18 changes: 18 additions & 0 deletions ArrayTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import static org.junit.Assert.*;
import org.junit.*;

public class ArrayTests {
@Test
public void testReverseInPlace() {
int[] input1 = { 3 };
ArrayExamples.reverseInPlace(input1);
assertArrayEquals(new int[]{ 3 }, input1);
}


@Test
public void testReversed() {
int[] input1 = { };
assertArrayEquals(new int[]{ }, ArrayExamples.reversed(input1));
}
}
55 changes: 55 additions & 0 deletions FileExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileExample {

/*
Given a File (which can represent either a file or directory), return a list
of all the files in that directory and all its subdirectories.
If given the path of a file (rather than a directory), it returns a list with
just that element.
For example, for the following structure:
some-files/
|- a.txt
|- more-files/
|- b.txt
|- c.java
|- even-more-files/
|- d.java
|- a.txt
Given new File("some-files/") as a parameter, we'd expect [some-files/a.txt,
some-files/more-files/b.txt, some-files/more-files/c.java,
some-files/even-more-files/d.java, some-files/even-more-files/a.txt] as results
Given new File("some-files/more-files") as a parameter, we'd expect
[some-files/more-files/b.txt, some-files/more-files/c.java] as results
Given new File("some-files/a.txt") as a parameter, we'd expect
[some-files/a.txt] and a result
See the File documentation here: https://docs.oracle.com/javase/8/docs/api/java/io/File.html
*/

static List<File> getFiles(File start) throws IOException {
File f = start;
List<File> result = new ArrayList<>();
result.add(start);
if(f.isDirectory()) {
File[] paths = f.listFiles();
for(File subFile: paths) {
result.add(subFile);
}
}
return result;
}
}


39 changes: 39 additions & 0 deletions LectureExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class MainExample1 {
public static void main(String[] args) {
int current = 0;
while(current < args.length) {
System.out.println(args[current]);
}
}
}

class MainExample2 {
public static void main(String[] args) {
int length = args.length;
for(int i = 0; i < length; length += 1) {
System.out.println(args[i]);
}
}
}

class EvensExample {
static int sumEvenIndices(int[] nums) {
int sum = 0;
for(int i = 0; i < nums.length; i += 2) {
sum += nums[i + 1];
}
return sum;
}
}

class NumsExample {
static double reciprocal(int n) {
return 1 / n;
}
static double ratio(int n, int d) {
return n / d;
}
static double formula(int a, int b, int c) {
return ratio(a, b) * reciprocal(c);
}
}
91 changes: 91 additions & 0 deletions LinkedListExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

import java.util.NoSuchElementException;

class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
class LinkedList {
Node root;
public LinkedList() {
this.root = null;
}
/**
* Adds the value to the _beginning_ of the list
* @param value
*/
public void prepend(int value) {
// Just add at the beginning
this.root = new Node(value, this.root);
}
/**
* Adds the value to the _end_ of the list
* @param value
*/
public void append(int value) {
if(this.root == null) {
this.root = new Node(value, null);
return;
}
// If it's just one element, add if after that one
Node n = this.root;
if(n.next == null) {
n.next = new Node(value, null);
return;
}
// Otherwise, loop until the end and add at the end with a null
while(n.next != null) {
n = n.next;
n.next = new Node(value, null);
}
}
/**
* @return the value of the first element in the list
*/
public int first() {
return this.root.value;
}
/**
* @return the value of the last element in the list
*/
public int last() {
Node n = this.root;
// If no such element, throw an exception
if(n == null) { throw new NoSuchElementException(); }
// If it's just one element, return its value
if(n.next == null) { return n.value; }
// Otherwise, search for the end of the list and return the last value
while(n.next != null) {
n = n.next;
}
return n.value;
}
/**
* @return a string representation of the list
*/
public String toString() {
Node n = this.root;
String s = "";
while(n != null) {
s += n.value + " ";
n = n.next;
}
return s;
}
/**
* @return the number of elements in the list
*/
public int length() {
Node n = this.root;
int i = 0;
while(n != null) {
i += 1;
n = n.next;
}
return i;
}
}
49 changes: 49 additions & 0 deletions ListExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.ArrayList;
import java.util.List;

interface StringChecker { boolean checkString(String s); }

class ListExamples {

// Returns a new list that has all the elements of the input list for which
// the StringChecker returns true, and not the elements that return false, in
// the same order they appeared in the input list;
static List<String> filter(List<String> list, StringChecker sc) {
List<String> result = new ArrayList<>();
for(String s: list) {
if(sc.checkString(s)) {
result.add(0, s);
}
}
return result;
}


// Takes two sorted list of strings (so "a" appears before "b" and so on),
// and return a new list that has all the strings in both list in sorted order.
static List<String> merge(List<String> list1, List<String> list2) {
List<String> result = new ArrayList<>();
int index1 = 0, index2 = 0;
while(index1 < list1.size() && index2 < list2.size()) {
if(list1.get(index1).compareTo(list2.get(index2)) < 0) {
result.add(list1.get(index1));
index1 += 1;
}
else {
result.add(list2.get(index2));
index2 += 1;
}
}
while(index1 < list1.size()) {
result.add(list1.get(index1));
index1 += 1;
}
while(index2 < list2.size()) {
result.add(list2.get(index2));
index1 += 1;
}
return result;
}


}
20 changes: 20 additions & 0 deletions MethodsTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import static org.junit.Assert.*;
import org.junit.*;

public class MethodsTests {
@Test
public void testSumEvensLength4() {
int[] input1 = { 12, 13, 7, 2};
assertEquals(EvensExample.sumEvenIndices(input1), 19);
}
@Test
public void testSumEvenLength5() {
int[] input1 = { 12, 13, 7, 2, 33};
assertEquals(EvensExample.sumEvenIndices(input1), 52);
}
@Test
public void testSumEvenLength6() {
int[] input1 = { 12, 13, 7, 8, 5, 3};
assertEquals(EvensExample.sumEvenIndices(input1), 24);
}
}
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.13.2.jar
Binary file not shown.

0 comments on commit 019159b

Please sign in to comment.