Skip to content
164 changes: 152 additions & 12 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.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -12,7 +14,21 @@ 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 oddSum = 0;

for (int num : nums) {
if (num % 2 != 0) {
oddSum += num;
}
}

if (oddSum == 0) {
return 0;
}

return oddSum;
}

/**
Expand All @@ -27,7 +43,23 @@ 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("Set is null");

if (words.isEmpty()) throw new IllegalArgumentException("Set is empty");

String shortestWord = "";

for (String word : words) {
if (shortestWord.equals("") || word.length() < shortestWord.length()) {
shortestWord = word;

} else if (word.length() == shortestWord.length() && word.compareTo(shortestWord) < 0) {
shortestWord = word;
}
}

return shortestWord;
}

/**
Expand All @@ -40,7 +72,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("Ages is null");

Set<String> names = new HashSet<>();

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

/**
Expand All @@ -51,7 +93,19 @@ 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("Head is null");

ListNode<Integer> current = head;

int biggest = Integer.MIN_VALUE;

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

/**
Expand All @@ -68,7 +122,20 @@ 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> freqMap = new HashMap<>();

ListNode<T> current = head;

while (current != null) {
if (!freqMap.containsKey(current.data)) {
freqMap.put(current.data, 1);
} else {
int count = freqMap.get(current.data) + 1;
freqMap.put(current.data, count);
}
current = current.next;
}
return freqMap;
}


Expand All @@ -81,7 +148,17 @@ 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 leftLevelCount = levelCount(root.left);

int rightLevelCount = levelCount(root.right);

if (leftLevelCount > rightLevelCount) {
return 1 + leftLevelCount;
} else {
return 1 + rightLevelCount;
}
}


Expand Down Expand Up @@ -109,7 +186,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;

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

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

return rightLevelSum + leftLevelSum;
}


Expand All @@ -124,7 +209,30 @@ 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 = sumTree(root);
int listSum = sumList(head);

return treeSum == listSum;
}

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

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

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

int sum = 0;

ListNode<Integer> current = head;

while (current != null) {
sum += current.data;
current = current.next;
}
return sum;
}

/**
Expand All @@ -136,7 +244,14 @@ public static boolean sumMatch(BinaryTreeNode<Integer> root, ListNode<Integer> h
* @return the sum of all the tree's values
*/
public static int nbSum(TreeNode<Integer> root) {
return 0;
if (root == null) return 0;

int sum = root.data;

for (TreeNode<Integer> child : root.children) {
sum += nbSum(child);
}
return sum;
}

/**
Expand Down Expand Up @@ -168,7 +283,15 @@ public static int nbSum(TreeNode<Integer> root) {
* @return the count of nodes that do not have siblings, EXCLUDING THE ROOT
*/
public static int onlyChildCount(TreeNode<?> root) {
return 0;
if (root == null) return 0;
int count = 0;
for (TreeNode<?> child : root.children) {
if (root.children.size() == 1) {
count++;
}
count += onlyChildCount(child);
}
return count;
}

/**
Expand Down Expand Up @@ -205,7 +328,24 @@ public static int onlyChildCount(TreeNode<?> root) {
* @param root the root value of the tree
* @return the depth of the tree, or 0 if the tree is null or the root is not present in the tree
*/
public static <T> int maxDepth(Map<T, List<T>> tree, T root) {
return 0;
public static <T> int maxDepth(Map<T, List<T>> tree, T root) {
if (tree == null || root == null || !tree.containsKey(root)) return 0;
return maxDepthHelper(tree, root);
}

private static <T> int maxDepthHelper(Map<T, List<T>> tree, T node) {
List<T> children = tree.get(node);

if (children == null || children.isEmpty()) {
return 1;
}

int deepest = 0;

for (T child : children) {
int childDepth = maxDepthHelper(tree, child);
deepest = Math.max(deepest, childDepth);
}
return 1 + deepest;
}
}