Skip to content
142 changes: 131 additions & 11 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Practice {
Expand All @@ -11,7 +15,18 @@ public class Practice {
* @return the sum of the odd numbers in the array
*/
public static int oddSum(int[] nums) {
return 0;
int sum = 0;
if (nums == null) {
return 0;
}

for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 != 0) {
sum+=nums[i];
}
}

return sum;
}

/**
Expand All @@ -26,7 +41,19 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;

if (words == null) throw new NullPointerException();
if (words.isEmpty()) throw new IllegalArgumentException();

String shortest = null;
for (String word : words) {
if (shortest == null) shortest = word;
if (word.length() < shortest.length()) shortest = word;
if (word.length() == shortest.length() && word.compareTo(shortest) < 0) shortest = word;
}
return shortest;


}

/**
Expand All @@ -39,7 +66,16 @@ public static String shortestWord(Set<String> words) {
* @throws NullPointerException if ages is null
*/
public static Set<String> adults(Map<String, Integer> ages) {
return null;
if (ages == null) throw new NullPointerException();

Set<String> result = new HashSet<>();
for (String name : ages.keySet()) {
if (ages.get(name) >= 18) {
result.add(name);
}
}
return result;

}

/**
Expand All @@ -50,7 +86,18 @@ public static Set<String> adults(Map<String, Integer> ages) {
* @throws IllegalArgumentException if head is null
*/
public static int biggestNumber(ListNode<Integer> head) {
return 0;
if (head == null) throw new IllegalArgumentException();

int max = head.data;
ListNode<Integer> current = head.next;

while (current != null) {
if (current.data > max) {
max = current.data;
}
current = current.next;
}
return max;
}

/**
Expand All @@ -67,7 +114,21 @@ public static int biggestNumber(ListNode<Integer> head) {
* @return a frequency map of values in the list
*/
public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
return null;
Map<T, Integer> frequency = new HashMap<>();
if (head == null) return frequency;

ListNode<T> current = head;
while (current != null) {
T data = current.data;
if (!frequency.containsKey(data)) {
frequency.put(data, 1);
} else {
frequency.put(data, frequency.get(data) + 1);
}
current = current.next;
}
return frequency;

}


Expand All @@ -80,7 +141,12 @@ public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
* @return the number of levels in the tree
*/
public static int levelCount(BinaryTreeNode<?> root) {
return 0;
if (root == null) return 0;

int left = levelCount(root.left);
int right = levelCount(root.right);

return 1 + Math.max(left, right);
}


Expand Down Expand Up @@ -108,7 +174,13 @@ public static int levelCount(BinaryTreeNode<?> root) {
* @return the sum of the nodes at the given level
*/
public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
return 0;
if(root == null) return 0;
// at this level, return value
if(level == 1) return root.data;

int sum = sumAtLevel(root.left, level -1) + sumAtLevel(root.right, level -1);

return sum;
}


Expand All @@ -123,8 +195,27 @@ public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
* @return true if the sums are equal, false otherwise
*/
public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> head) {
return false;
int treeTotal = treeSum(root);
int listTotal = listSum(head);
return treeTotal == listTotal;
}

private static int treeSum(BinaryTreeNode<Integer> root) {
if (root == null) return 0; // empty tree = 0
return root.data + treeSum(root.left) + treeSum(root.right);
}

private static int listSum(ListNode<Integer> head) {
int sum = 0;
ListNode<Integer> current = head;
while (current != null) {
sum += current.data;
current = current.next;
}
return sum;
}



/**
* Returns the sum of all the vertices in a graph that are reachable from a given
Expand All @@ -136,8 +227,22 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h
* @return the sum of all the vertices
*/
public static int graphSum(Vertex<Integer> start) {
return 0;

if (start == null) return 0;
Set<Vertex<Integer>> visited = new HashSet<>();
return dfsSum(start, visited);
}

private static int dfsSum(Vertex<Integer> v, Set<Vertex<Integer>> visited) {
// already visited
if (!visited.add(v)) return 0;
int sum = v.data;
for (Vertex<Integer> next : v.neighbors) {
sum += dfsSum(next, visited);
}
return sum;

}

/**
* Returns the count of vertices in a graph that have an outdegree of 0.
Expand All @@ -148,6 +253,21 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
}
if (start == null) return 0;

int count = 0;
Set<Vertex<Integer>> seen = new HashSet<>();
Queue<Vertex<Integer>> skin1 = new LinkedList<>();
skin1.add(start);

while (!skin1.isEmpty()) {
Vertex<Integer> node = skin1.poll();
if (!seen.add(node)) continue;

if (node.neighbors == null || node.neighbors.isEmpty()) count++;
else skin1.addAll(node.neighbors);
}
return count;
}

}