Skip to content
Open
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
151 changes: 142 additions & 9 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,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 +39,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) throw new NullPointerException("Words was not created");
if (words.isEmpty()) throw new IllegalArgumentException("Words is empty");
String shortest = null;
for (String word : words) {
if (shortest == null) shortest = word;
if (shortest.length() > word.length()) {
shortest = word;
} else if (shortest.length() == word.length()) {
if (word.compareTo(shortest) < 0) {
shortest = word;
}
}
}

return shortest;
}

/**
Expand All @@ -39,7 +66,17 @@ 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> adults = new HashSet<String>();

for (String person : ages.keySet()) {
if (ages.get(person) >= 18) {
adults.add(person);
}
}

return adults;
}

/**
Expand All @@ -50,7 +87,17 @@ 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,9 @@ 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;

return 1 + Integer.max(levelCount(root.left), levelCount(root.right));
}


Expand Down Expand Up @@ -108,9 +171,18 @@ 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;

return traverse(root, level, 1);
}

public static int traverse(BinaryTreeNode<Integer> root, int targetLevel, int currentLevel) {
if (root == null) return 0;

if (currentLevel == targetLevel) return root.data;

return traverse(root.left, targetLevel, currentLevel+1) + traverse(root.right, targetLevel, currentLevel+1);
}


/**
* Returns true if the sum of the values in a given tree is equal to the sum
Expand All @@ -123,9 +195,33 @@ 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) {
int treeTotal = traverseTreeSum(root);
int listTotal = traverseListSum(head);

if (treeTotal == listTotal) return true;

return false;
}

public static int traverseTreeSum(BinaryTreeNode<Integer> root) {
if (root == null) return 0;

return root.data + traverseTreeSum(root.left) + traverseTreeSum(root.right);
}

public static int traverseListSum(ListNode<Integer> head) {
if (head == null) return 0;

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

return total;
}

/**
* Returns the sum of all the vertices in a graph that are reachable from a given
* starting vertex.
Expand All @@ -136,7 +232,26 @@ 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 graphSum(start, visited);
}

public static int graphSum(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
if (current == null || visited.contains(current)) return 0;

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

if (current.neighbors == null) return sum;

for (Vertex<Integer> vertex : current.neighbors) {
if (vertex != null) sum+=graphSum(vertex, visited);
}

return sum;
}

/**
Expand All @@ -148,6 +263,24 @@ 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;

Set<Vertex<Integer>> visited = new HashSet<>();

return sinkCount(start, visited);
}

public static int sinkCount(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
if (current == null || visited.contains(current)) return 0;

visited.add(current);
if (current.neighbors == null || current.neighbors.isEmpty()) return 1;

int exitCount = 0;
for (Vertex<Integer> vertex : current.neighbors) {
exitCount+=sinkCount(vertex, visited);
}

return exitCount;
}
}