Skip to content
155 changes: 144 additions & 11 deletions src/Practice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Practice {
Expand All @@ -12,7 +16,14 @@ 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 num: nums) {
if (num % 2 != 0) {
sum += num;
}
}
return sum;
}

/**
Expand All @@ -27,7 +38,24 @@ public static int oddSum(int[] nums) {
* @throws NullPointerException if words is null
*/
public static String shortestWord(Set<String> words) {
return null;
//"kumquat", "date", "fig", "grape", "cherry"
if (words == null) { throw new NullPointerException(); }
if (words.isEmpty()) { throw new IllegalArgumentException(); }

int length = Integer.MAX_VALUE;
String shortest = "";
for(String word: words){
if (word.length() < length) {
shortest = word;
length = word.length();
} else if (word.length() == length) {
if (word.compareTo(shortest) < 0) {
shortest = word;
}
}
}

return shortest;
}

/**
Expand All @@ -40,7 +68,14 @@ 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> adult = new HashSet<>();
for(String age: ages.keySet()){
if (ages.get(age) >= 18) {
adult.add(age);
}
}
return adult;
}

/**
Expand All @@ -51,7 +86,14 @@ 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();}
ListNode<Integer> current = head;
int max = head.data;
while (current != null) {
max = Math.max(max, current.data);
current = current.next;
}
return max;
}

/**
Expand All @@ -68,7 +110,18 @@ 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;
if (head == null) { return new HashMap<>();}
Map<T, Integer> frequency = new HashMap<>();
ListNode<T> current = head;
while (current != null) {
if (!frequency.containsKey(current.data)) {
frequency.put(current.data, 1);
} else {
frequency.put(current.data, frequency.get(current.data) + 1);
}
current = current.next;
}
return frequency;
}


Expand All @@ -81,7 +134,10 @@ 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 leftTree = levelCount(root.left) + 1;
int leftRight = levelCount(root.right) + 1;
return Math.max(leftTree, leftRight);
}


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

Queue<BinaryTreeNode<Integer>> q = new LinkedList<>(); // 1
int sum = 0; // 7 + 9 + 2
int L = 1; // 3
q.add(root);

while(!q.isEmpty()){
for(int i = q.size(); i > 0; i--){
BinaryTreeNode<Integer> current = q.poll(); // 7 9 2
if (L == level) {
sum += current.data;
}

if (current.left != null){ q.add(current.left);}
if (current.right != null){ q.add(current.right);}
}
L += 1;
}
return sum;
}


Expand All @@ -124,7 +199,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 rootSum = sumMatchHelper(root);
int listSum = sumMatchHelper(head);
return rootSum == listSum ? true : false;
}

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

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

/**
Expand All @@ -136,7 +231,12 @@ 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 +268,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 +313,32 @@ 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
*/
/*
/*
Map: Tree:
A -> [B, E, C] A
B -> [E] / | \
E -> [] B E C
C -> [D, Q] | / \
D -> [Z] E D Q
Q -> [] \
Z -> [] Z
*/

public static <T> int maxDepth(Map<T, List<T>> tree, T root) {
return 0;
if (root == null || tree == null || !tree.containsKey(root)) return 0;
int depth = maxDepthHelper(tree, root);
return depth;
}

public static <T> int maxDepthHelper(Map<T, List<T>> tree, T root){
if(!tree.containsKey(root)) return 1;
int depth = 1;
List<T> children = tree.get(root);

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