Skip to content
Open
147 changes: 137 additions & 10 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

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

int sum = 0;

for (int i : nums) {
if (i % 2 != 0){
sum = sum + i;
}
i++;
}
return sum;
}

/**
Expand All @@ -26,7 +40,21 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
if (words == null || words.isEmpty()){
throw new IllegalArgumentException("Set is empty");
}

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

/**
Expand All @@ -39,7 +67,18 @@ 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> names = new HashSet<>();

for (String name : ages.keySet()){
if(ages.get(name) >= 18){
names.add(name);
}
}
return names;
}

/**
Expand All @@ -50,7 +89,20 @@ 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 NullPointerException();
}

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

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

/**
Expand All @@ -67,7 +119,17 @@ 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<>();

ListNode<T> current = head;

while (current != null) {
T value = current.data;
frequency.put(value, frequency.getOrDefault(value, 0) + 1);
current = current.next;
}

return frequency;
}


Expand All @@ -80,7 +142,14 @@ 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 leftLevels = levelCount(root.left);
int rightLevels = levelCount(root.right);

return 1 + Math.max(leftLevels, rightLevels);
}


Expand Down Expand Up @@ -108,7 +177,15 @@ 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;
}

if (level == 1){
return root.data;
}

return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1);
}


Expand All @@ -123,7 +200,23 @@ 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 treeSum = sumTreeHelper(root);

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

return treeSum == listSum;
}

private static int sumTreeHelper(BinaryTreeNode<Integer> node){
if (node == null){
return 0;
}
return node.data + sumTreeHelper(node.left) + sumTreeHelper(node.right);
}

/**
Expand All @@ -136,7 +229,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;
Set<Vertex<Integer>> visited = new HashSet<>();
return dfsSumHelper(start, visited);
}

private static int dfsSumHelper(Vertex<Integer> vertex, Set<Vertex<Integer>> visited){
if (vertex == null || visited.contains(vertex)){
return 0;
}

visited.add(vertex);
int sum = vertex.data;

for (Vertex<Integer> neighbor : vertex.neighbors){
sum += dfsSumHelper(neighbor, visited);
}
return sum;
}

/**
Expand All @@ -148,6 +256,25 @@ public static int graphSum(Vertex<Integer> start) {
* @return the count of vertices with outdegree 0
*/
public static int sinkCount(Vertex<Integer> start) {
return 0;
Set<Vertex<Integer>> visited = new HashSet<>();
return dfsSinkCountHelper(start, visited);
}

private static int dfsSinkCountHelper(Vertex<Integer> vertex, Set<Vertex<Integer>> visited) {
if (vertex == null || visited.contains(vertex)){
return 0;
}

visited.add(vertex);

int count = 0;
if (vertex.neighbors.isEmpty()){
count = 1;
}

for (Vertex<Integer> neighbor : vertex.neighbors) {
count += dfsSinkCountHelper(neighbor, visited);
}
return count;
}
}