Skip to content

updated stack with passing tests #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ArrayListCombiner;

import java.util.ArrayList;
import java.util.Collections;

/**
* Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items,
Expand All @@ -9,4 +10,12 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {

public static <E> void extendCombiner(ArrayList<E> list1 ,ArrayList<? extends E> list2) {
list1.addAll(list2);
}

public static <E> void superCombiner(ArrayList<? super E> list1, ArrayList<E> list2){
list1.addAll(list2);
}
}
9 changes: 8 additions & 1 deletion src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
* Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
* and returns an ArrayList with all of the elements of the first after the function is applied to them.
*/
public class MapFunc {
public class MapFunc{

public static<T,R> ArrayList<R> map(ArrayList<T> first, Function<T, R> second) {
ArrayList<R> result = new ArrayList<R>();
for (T object : first) result.add(second.apply(object));

return result;
}

}
34 changes: 33 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@
* And a minmax method that returns a pair containing the largest and smallest items from the array list
*/
public class Arrays {
public static <___> Pair<E> firstLast(ArrayList<___> a) {
public static <E> Pair<E> firstLast(ArrayList<E> a) {
E t1 = a.get(0);
E t2 = a.get(a.size() - 1);
Pair<E> thePair = new Pair<E>(t1, t2);
return thePair;
}

public static <E> Pair<E> minMax(ArrayList<E> a) {
Pair<E> pair = new Pair<E>(min(a), max(a));
return pair;
}

public static <E> E min(ArrayList<E> a) {
E min = a.get(0);
Pair<E> thePair;
for (int i = 1; i < a.size(); i++) {
thePair = new Pair<E>(min, a.get(i));
min = thePair.min();

}
return min;
}

public static <E> E max(ArrayList<E> a) {

E max = a.get(0);
Pair<E> thePair;
for (int i = 0; i < a.size(); i++) {
thePair = new Pair<E>(max, a.get(i));
max = thePair.max();
}
return max;
}

}
47 changes: 46 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,57 @@
package Pair;

import java.util.ArrayList;
import java.util.Collections;

/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* getFirst
* getSecond
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E> {
private E type1;
private E type2;

Pair(E theType1, E theType2) {
this.type1 = theType1;
this.type2 = theType2;
}

public E getFirst() {
return this.type1;
}


public E getSecond() {
return this.type2;
}

public <E extends Comparable<E>> E min() {
E type1 = (E) this.getFirst();
E type2 = (E) this.getSecond();

E min = type1;

if (type2 != null && min == null || type2.compareTo(min) < 0) {
min = type2;
}

return min;

}

public <E extends Comparable<E>> E max() {
E type1 = (E) this.getFirst();
E type2 = (E) this.getSecond();
E max = type1;

if (type2 != null && max == null || type2.compareTo(max) > 0) {
max = type2;
}

return max;

}
}
83 changes: 83 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,98 @@
package StackArray;

import java.util.Arrays;
import java.util.Stack;

/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class GenericStack<E> {
private static int DEFAULT_SIZE = 10;
private E[] elements;
private int size;
private int numberOfNulls;

public GenericStack() {
this.elements = (E[]) new Object[DEFAULT_SIZE];
this.size = 0;
this.numberOfNulls = 0;
}

public void push(E object) {
add(object);
}


public boolean isEmpty() {
return size == 0;
}

public E pop() throws IndexOutOfBoundsException {
if (this.isEmpty()) {
throw new IndexOutOfBoundsException("this list is empty");
}
return remove();
}


public boolean add(E object) {
ensureCapasity(size + 1);
this.elements[size++] = object;
//size++;
return true;
}

public int indexOf(E theObject) {
for (int i = 0; i < size; i++) {
if (theObject.equals(elements[i])) {
return i;
}
}
return -1;
}

public void shift(int index) {
E theObject = null;
for (int i = index; i < size; i++) {
theObject = elements[i];
if (elements[i + 1] != null)
elements[i + 1] = theObject;
}
}

public void ensureCapasity(int size) {
if (size - elements.length > 0) {
grow();
}
}

public void grow() {
int newCapasity = elements.length + DEFAULT_SIZE;
Arrays.copyOf(elements, newCapasity);
}

public E remove() {
E removedObject = elements[size -1];
elements[size -1] = null;
//numberOfNulls++;
//checkToResize(numberOfNulls);
size--;
return removedObject;
}

public void checkToResize(int theNumber) {
int length = elements.length - DEFAULT_SIZE;
if (length > 10 && theNumber == 10) {
resize(length);
}
}

public void resize(int minLength) {
elements = Arrays.copyOf(elements, minLength);
}


}
83 changes: 83 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,95 @@
/**
* Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class ObjectStack<E> {
private int nullValues;
private int size;
private static final int DEFAULT_SIZE = 10;
private Object[] elements;

public ObjectStack() {
elements = new Object[DEFAULT_SIZE];
size = 0;
nullValues = 0;
}

public void push(E object) {
add(object);
}


public boolean isEmpty() {
return size == 0;
}

public Object pop() throws IndexOutOfBoundsException {
if (this.isEmpty()) {
throw new IndexOutOfBoundsException("this list is empty");
}
return remove();
}


public boolean add(Object object) {
ensureCapasity(size + 1);
this.elements[size++] = object;
//size++;
return true;
}

public int indexOf(Object theObject) {
for (int i = 0; i < size; i++) {
if (theObject.equals(elements[i])) {
return i;
}
}
return -1;
}

public void shift(int index) {
Object theObject = null;
for (int i = index; i < size; i++) {
theObject = elements[i];
if (elements[i + 1] != null)
elements[i + 1] = theObject;
}
}

public void ensureCapasity(int size) {
if (size - elements.length > 0) {
grow();
}
}

public void grow() {
int newCapasity = elements.length + DEFAULT_SIZE;
Arrays.copyOf(elements, newCapasity);
}

public Object remove() {
Object removedObject = elements[size - 1];
elements[size - 1] = null;
//numberOfNulls++;
//checkToResize(numberOfNulls);
size--;
return removedObject;
}

public void checkToResize(int theNumber) {
int length = elements.length - DEFAULT_SIZE;
if (length > 10 && theNumber == 10) {
resize(length);
}
}

public void resize(int minLength) {
elements = Arrays.copyOf(elements, minLength);
}


}


26 changes: 24 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;
private ArrayList<E> elements;
// private Object the;

public Stack() {
elements = new ArrayList();
}

// pushes the object to top of stack. works like add method but to the front instead of end
public void push(E item) {
int index = 0;
elements.add(index, item);
}

public Stack(){
public boolean isEmpty() {
return elements.size() == 0;
}

public E pop()throws IndexOutOfBoundsException {
if(elements.isEmpty()) throw new IndexOutOfBoundsException("this list is empty");
int index = 0;
E removedObject = elements.get(0);
elements.remove(index);
return removedObject;
}




}
Loading