Test Methods Flashcards

1
Q

Row Lengths Decrease

int[][] t

A
public static boolean rowLengthsDecrease(int[][] t){
	//start with single for loop to target each row
	for (int i = 0; i < t.length -1; i++){
		if (t[i].length < t[i+1].length){
			return false;
		}
	}
	return true;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Row Values Increase

int[][] t

A
public static boolean rowValuesIncrease(int[][] t){
	for (int i=0; i < t.length-1; i++){
		for (int j=0; j < t[i].length-1; j++){
			if (t[i][j] >= t[i][j+1]){
				return false;
			}
		}
	}
	return true;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Column Values Increase

int[][] t

A
public static boolean columnValuesIncrease(int[][] t){
	for (int i=1; i < t.length; i++){
		for (int j= 0; j < t[i].length; j++){
			if (t[i-1][j] >= t[i][j]){
				return false;
			}
		}
	}
	return true
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is Set of 1 to N

int[][] t

A
public static boolean isSetOf1toN(int[][] t){
	//3 key stages
	//stage 1: get cell count of t 
	int cellCount = 0;
	for (int i = 0; i < t.length; i++){
		cellCount += t[i].length;
	}		
	// stage 2: create array of booleans to check if
	// any cell values are > no. of cells (they can't be)
	boolean[] s = new boolean[cellCount];
	for (int i=0; i < t.length; i++){
		for (int j=0; j < t[i].length; j++){
			int cellValue = t[i][j];
			if (cellValue > s.length){
				return false;	
			}
			s[cellValue -1] = true;
		}
	}
	for (Boolean value : s){
		if (!value){
			return false;
		}
	}
	return true;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Add Value (Week 8)
(Integer value)
A
public void addValue(Integer value){
	if (smallest == null){
		smallest = new Cell(value);
		return;							
	}
    Cell bump = smallest;
    value = addToRow(smallest, value);
	while (value != null){
		if (bump.below == null){
			bump.below = new Cell(value);
			bump.below.above = bump;
			return; 
		}
		bump = bump.below;
		value = addToRow(bump, value);
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Add to Row (Week 8)

Cell curr, int value

A

public Integer addToRow(Cell curr, int value){
int current = value;
while (current > curr.value && curr.right != null){
curr = curr.right;
}

	while (curr.value > current){
		current = curr.value;
		curr.value = value;
		return current;
	}
	if (curr.right == null){
		curr.right = new Cell(value);
		curr.right.left = curr;
		if (curr.above != null){
			curr.right.above = curr.above.right;
		}
	}
	return null;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Size (Week 10)

A
public int size(){
	if (nodeValue == null){
		return 0;
	}
	int countNodes = 1;
	for (Tree child : children){
		countNodes += child.size();
	}
	return countNodes;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Max Degree (Week 10)

A

public int maxDegree(){
if (children.isEmpty()){
return 0;
}
int[] widths = new int[children.size() + 1];
widths[children.size()] = children.size();

for (int i = 0; i < children.size(); i++){
	widths[i] = children.get(i).maxDegree();
}
	int maxWidth = 0;
	for (int width : widths){
		if (width > maxWidth){
			maxWidth = width;
		}
	}
	return maxWidth;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Post Order (Week 10)
Returns a List
A
public List postOrder(){
	ArrayList nodes = new ArrayList();
	if (children.isEmpty()){
		nodes.add(rootValue);
		return nodes;
	}
	for (Tree child : children){
		nodes.addAll(child.postOrder());
	}
	nodes.add(rootValue);
	return nodes;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To Indented String (Week 10)

A
public String toIndentedString(){
	String indentTree = rootValue + "\n";
	if (children.isEmpty()){
		return indentTree;
	}
	String childStr = "";
	for (Tree child : children){
		childStr += child.toIndentedString();
	}
	String[] childArr = childStr.split("\n");
	for (String child : childArr){
		indentTree += "  " + child + "\n";
	}
	return indentTree;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Add (Week 11)

T element

A

public void add(T element){
if (root == null){
root = element;
}

int comparison = root.compareTo(element);
	if (comparison > 0){
		if (left == null){
			left = new LinkedBST(element);
		} else {
			left.add(element);
		}
	} else if (comparison < 0) {
		if (right == null){
			right = new LinkedBST(element);
		} else {
			right.add(element);
		}
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Search (Week 11)

T element

A

public boolean search(T target){
if (root == null){
return false;
}

int comparison = root.compareTo(target);
if (comparison == 0){
	return true;
} else if (comparison > 0 &amp;&amp; left != null){
	return left.search(target);
} else if (comparison < 0 &amp;&amp; right != null){
	return right.search(target);
}

return false; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Height (Week 11)

A

public int height(){
if (root == null){
return 0;
}

	int lTotal = 0;
	int rTotal = 0;
	if (left != null){
		lTotal += 1 + left.height();
	}
	if (right != null){
		rTotal = 1 + right.height();
	}
return Math.max(lTotal, rTotal); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Size (Week 11)

A

public int size(){
if (root == null){
return 0;
}

	int total = 1;		
	if (left != null){
		total += left.size();
	}
	if (right != null){
		total += right.size();
	}
return total; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Size Above (Week 11)
(T low)
A

public int sizeAbove(T low){
if (root == null){
return 0;
}

int count = 0;
int comparison = root.compareTo(low);

if (comparison >= 0){
	count++;
}
	if (right != null){
		count += right.sizeAbove(low);
	}
	if (left != null){
		count += left.sizeAbove(low);
	}
return count; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
Size Below (Week 11)
(T high)
A

public int sizeBelow(T high){
int result = size() - sizeAbove(high);
return result;
}

17
Q
Size Between (Week 11)
(T low, T high)
A
public int sizeBetween(T low, T high){
	if (root == null){
		return 0;
	}
	int result = size() - (sizeAbove(high) + sizeBelow(low));
    return result; }