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
221 changes: 194 additions & 27 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 sum;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 != 0) {
sum += nums[i];
}
}
return sum;
}

/**
Expand All @@ -23,10 +34,35 @@ 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.isEmpty()) {
throw new IllegalArgumentException();
}
String shortWord = "";
for (String word : words) {
if (word == null) {
throw new NullPointerException();
}
if (shortWord.equals("") || word.length() < shortWord.length()) {
shortWord = word;
} else if (word.length() == shortWord.length()) {

for (int i = 0; i < word.length(); i++) {

if (word.charAt(i) != shortWord.charAt(i)) {

if ((int) word.charAt(i) < (int) shortWord.charAt(i)) {
shortWord = word;
}
break;
}
}
}
}

return shortWord;
}

/**
Expand All @@ -39,7 +75,16 @@ 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> nameSet = new HashSet<>();
for (String name : ages.keySet()) {
if (ages.get(name) >= 18) {
nameSet.add(name);
}
}
return nameSet;
}

/**
Expand All @@ -50,26 +95,55 @@ 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 = Integer.MIN_VALUE;
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> frequencyMap = new HashMap<>();
if (head == null) {
return frequencyMap;
}

ListNode<T> current = head;
while (current != null) {
if (frequencyMap.containsKey(current.data)) {
int count = frequencyMap.get(current.data);
count += 1;
frequencyMap.put(current.data, count);
} else {
frequencyMap.put(current.data, 1);
}
current = current.next;
}

return frequencyMap;
}

/**
* Returns the number of levels in the tree.
Expand All @@ -80,41 +154,69 @@ 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;
}
int levels = 0;
if (root == null) {
return levels;
}
levels++;

int maxDepth = Math.max(levelCount(root.left), levelCount(root.right));
levels += maxDepth;

return levels;
}

/**
* 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) {
return 0;
}

return sumAtLevel(root, level, 1);
}

public static int sumAtLevel(BinaryTreeNode<Integer> root, int level, int currentLevel) {
int sum = 0;
if (root == null) {
return sum;
}

if (currentLevel == level) {
sum = root.data;
}

currentLevel++;
sum += sumAtLevel(root.left, level, currentLevel);
sum += sumAtLevel(root.right, level, currentLevel);

return sum;
}

/**
* 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 @@ -123,11 +225,40 @@ 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 lSum = 0;
int tSum = 0;
if (head != null) {
ListNode<Integer> current = head;
while (current != null) {
lSum += current.data;
current = current.next;
}
}
if (root != null) {
tSum = treeSum(root);
}

if (lSum == tSum) {
return true;
}
return false;
}

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

sum = root.data;
sum += treeSum(root.left);
sum += treeSum(root.right);
return sum;
}

/**
* Returns the sum of all the vertices in a graph that are reachable from a given
* Returns the sum of all the vertices in a graph that are reachable from a
* given
* starting vertex.
*
* Returns 0 if the starting vertex is null.
Expand All @@ -136,7 +267,23 @@ 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;
Set<Vertex<Integer>> visited = new HashSet<>();
return graphSum(start, visited);
}

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

visited.add(start);
sum += start.data;
for (Vertex<Integer> neighbor : start.neighbors) {
sum += graphSum(neighbor, visited);
}

return sum;
}

/**
Expand All @@ -148,6 +295,26 @@ 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> start, Set<Vertex<Integer>> visited) {
int total = 0;
if (start == null || visited.contains(start)) {
return 0;
}

visited.add(start);
if (start.neighbors.isEmpty()) {
total++;
}
for (Vertex<Integer> neighbor : start.neighbors) {
total += sinkCount(neighbor, visited);
}
return total;
}
}