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
170 changes: 159 additions & 11 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,16 @@ 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 num : nums) {
if (num % 2 != 0){
sum += num;
}
}
return sum;
}

/**
Expand All @@ -20,13 +31,27 @@ public static int oddSum(int[] nums) {
* If multiple words are tied for shortest, returns the one that is smallest
* lexicographically.
*
* @param words a set of words
* @param words a set of word
* @return the shortest word in the set with a lexicographic tiebreaker
* @throws IllegalArgumentException if words is empty
* @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 || word.length() < shortest.length()
|| (word.length() == shortest.length() && word.compareTo(shortest) < 0)) {
shortest = word;
}
}
return shortest;
}

/**
Expand All @@ -39,7 +64,19 @@ 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()) {
Integer age = ages.get(name);

if (age >= 18) {
result.add(name);
}
}
return result;
}

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

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 +117,22 @@ 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, frequency.get(data) + 1);
} else {
frequency.put(data, 1);
}
current = current.next;
}
return frequency;
}


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

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


Expand Down Expand Up @@ -108,7 +180,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;
if (root == null) {
return 0;
}

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

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

return left + right;
}


Expand All @@ -123,7 +206,32 @@ 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 = 0;

if (root != null) {
treeSum = root.data;
if (root.left != null) {
treeSum += sumMatchHelper(root.left);
}
if (root.right != null) {
treeSum += sumMatchHelper(root.right);
}
}

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

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

/**
Expand All @@ -136,7 +244,24 @@ 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 graphSumHelper(start, visited);
}

public static int graphSumHelper(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
if (visited.contains(current)) {
return 0;
}
visited.add(current);

int sum = current.data;
for (Vertex<Integer> neighbor : current.neighbors) {
sum += graphSumHelper(neighbor, visited);
}
return sum;
}

/**
Expand All @@ -148,6 +273,29 @@ 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 sinkCountHelper(start, visited);
}

public static int sinkCountHelper(Vertex<Integer> current, Set<Vertex<Integer>> visited) {
// visited vertex
if (visited.contains(current)) {
return 0;
}
visited.add(current);

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

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