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
260 changes: 209 additions & 51 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,18 @@ 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 @@ -24,10 +37,32 @@ public static int oddSum(int[] nums) {
* @param words a set of words
* @return the shortest word in the set with a lexicographic tiebreaker
* @throws IllegalArgumentException if words is empty
* @throws NullPointerException if words is null
* @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) {
shortest = word;
} else if (word.length() < shortest.length()) {
shortest = word;
} else if (word.length() == shortest.length()) {
if (word.compareTo(shortest) < 0) {
shortest = word;
}
}
}

return shortest;
}

/**
Expand All @@ -40,7 +75,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()) {
if (ages.get(name) >= 18) {
result.add(name);
}
}

return result;
}

/**
Expand All @@ -51,26 +98,53 @@ 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;

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

return biggest;
}

/**
* Returns a frequency map counting how frequently items appear in a linked list.
* Returns a frequency map counting how frequently items appear in a linked
* list.
*
* Example:
* Input: a -> x -> a -> a -> x -> y
* Output: {a:3, x:2, y:1}
* Input: a -> x -> a -> a -> x -> y
* Output: {a:3, x:2, y:1}
*
* Returns an empty map if head is null
*
* @param <T> the type of data held by the list
* @param <T> the type of data held by the list
* @param head the head of the list
* @return a frequency map of values in the list
*/
public static <T> Map<T, Integer> frequencies(ListNode<T> head) {
return null;
}
Map<T, Integer> result = new HashMap<>();

ListNode<T> current = head;

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

return result;
}

/**
* Returns the number of levels in the tree.
Expand All @@ -81,41 +155,54 @@ 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 leftCount = levelCount(root.left);
int rightCount = levelCount(root.right);

return Math.max(leftCount, rightCount) + 1;
}

/**
* Returns the sum at a specified level in a binary tree.
*
* For example, if the given level was 3:
* 5
* / \
* 8 4
* / \ /
* 7 9 2
* /
* 1
* 5
* / \
* 8 4
* / \ /
* 7 9 2
* /
* 1
*
* Nodes at level 3: 7, 9, and 2
* Sum of nodes at level 3: 18
* Sum of nodes at level 3: 18
*
* The root is considered to be at level 1.
*
* Returns 0 if the tree is empty or if the level is not present in the tree.
*
* @param root the root of the binary tree
* @param root the root of the binary tree
* @param level the level to sum
* @return the sum of the nodes at the given level
*/
public static int sumAtLevel(BinaryTreeNode<Integer> root, int level) {
return 0;
}
if (root == null || level <= 0) {
return 0;
}

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

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

/**
* Returns true if the sum of the values in a given tree is equal to the sum
* of the values in the given list.
* of the values in the given list.
*
* An empty tree or list is considered to have a sum of 0.
*
Expand All @@ -124,7 +211,26 @@ 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 listSum = 0;

ListNode<Integer> current = head;

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

int treeSum = sumTree(root);

return treeSum == listSum;
}

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

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

/**
Expand All @@ -136,22 +242,34 @@ 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;
}

/**
* Returns the count of nodes in a non-binary tree that are only children, EXCLUDING the root.
* Returns the count of nodes in a non-binary tree that are only children,
* EXCLUDING the root.
*
* In other words, how many nodes in the tree do NOT have siblings, NOT INCLUDING THE ROOT.
* In other words, how many nodes in the tree do NOT have siblings, NOT
* INCLUDING THE ROOT.
*
* Example:
* A
* / | \
* B C D
* / / \ |
* E F X G
* \
* H
* A
* / | \
* B C D
* / / \ |
* E F X G
* \
* H
*
* Only children: E, G, and H
* - E is an only child because B has exactly one child
Expand All @@ -168,28 +286,41 @@ 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;
}

/**
* Returns the maximum depth of the tree.
*
* Example map:
* {
* A=[B, C, D],
* B=[E, F],
* D=[G],
* G=[H]
* A=[B, C, D],
* B=[E, F],
* D=[G],
* G=[H]
* }
*
* Tree represented by the map:
* A
* / | \
* B C D
* / \ |
* E F G
* \
* H
* A
* / | \
* B C D
* / \ |
* E F G
* \
* H
*
* The longest path from the root to a leaf is:
* A -> D -> G -> H
Expand All @@ -200,12 +331,39 @@ public static int onlyChildCount(TreeNode<?> root) {
*
* The tree is represented as a map of parent values to lists of children.
*
* @param <T> the type of the data in the tree
* @param <T> the type of the data in the tree
* @param tree a map of parent values to lists of children
* @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
* @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;
if (tree == null || root == null || !tree.containsKey(root)) {
return 0;
}

List<T> children = tree.get(root);

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

int max = 0;

for (T child : children) {
int depth;

if (tree.containsKey(child)) {
depth = maxDepth(tree, child);
} else {
depth = 1;
}

if (depth > max) {
max = depth;
}
}

return max + 1;
}
}